Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
PLease help
its about recursion and multiplying the even numbers

What I have tried:

int fn(){
int n;
n = rand() % 900 + 100;
printf("%d\n", n);
return 0;
}
Posted
Updated 22-Apr-20 0:00am
Comments
Afzaal Ahmad Zeeshan 22-Apr-20 5:55am    
You forgot to mention the 2 questions as well as the problems you are having.
Patrice T 22-Apr-20 6:18am    
And the questions are ?

1 solution

There is no recursion in that code: if fn was recursive, then it wouldn't work as there is no way to "limit" it from infinite recursion - you really need to parameter to do that, or you app will just blow the top off the stack and crash.
This is recursive:
C++
int factorial(int n)
   {
   if (n <= 1) return 1;
   return n * factorial(n - 1);
   }
Because it calls the factorial function from within the factorial function. Because the parameter value changes each time it gets called, the limiting check of "one or less" prevents infinite recursion, and it finally produces the correct result.

I'd suggest you go back to your course notes for the last lecture, and your homework question and read them again, really carefully!
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900