+ 3
Is the below one is recursive or iterative program???
fact=1; num=5; while(num) { fact*=fact*(num--); } cout<<fact;
3 Answers
+ 3
iterative, because it uses a loop to iterate.
recursive needs a function calling itself.
+ 3
thank you a lot for the answer
+ 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đ