Is the below one is recursive or iterative program??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Is the below one is recursive or iterative program???

fact=1; num=5; while(num) { fact*=fact*(num--); } cout<<fact;

23rd Feb 2021, 6:04 PM
Ronak Paul
Ronak Paul - avatar
3 Answers
+ 3
iterative, because it uses a loop to iterate. recursive needs a function calling itself.
23rd Feb 2021, 6:25 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 3
thank you a lot for the answer
23rd Feb 2021, 6:31 PM
Ronak Paul
Ronak Paul - avatar
+ 1
When you use a loop, it become itterative. A recusion use (i) a start value and (ii) the return from the functions last call the new call. Like f(n) = f(n-1) , f(0) = b, n >= 0 I no expert in CCP but to me it looks like ’fact ’is a variable to which you multiply a value that decreace with one at every iterartion. And all this you multiply to the variable. I should not call that a recusive function. In your case you don’t need to ’start with the inner value to count you out’. You can just solve ut by a ordinary itterative process. If it should be a recursive function there must exist som kind of selcetion inside the code: is this the base case or or not. /Regards Per B🙂
23rd Feb 2021, 6:38 PM
Per Bratthammar
Per Bratthammar - avatar