0

I understand upto 3rd line. But how to comes the output don't understand . Plz explain.

int recursive (int num){ if(num==1) return 2; return num*recursive (num-1); } int main(){ printf("%d", recursive (5)); }

11th Sep 2020, 2:02 PM
Suparna Podder
Suparna Podder - avatar
4 Réponses
+ 3
Jayakrishna🇮🇳 sir i think Output should be 240 instead of 125 . This is recursive function you passing 5 then control will pass recursive function then it will check condition if cond is false so next return will work so return num*recursive (num-1) Means 5*(5-1) which will be 20 same Again here recursive function is calling and it will decrease by 1 again it will decrease when num will be 1 then if condition will work and it will multiply so 240 will print But if u write return 3 ; in place of return 2; then it will be 360. Hope you understood. ......... Thankyou.........
11th Sep 2020, 4:53 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
From 4 line 5 * ( recursive(5-1)) => 5 * (4 * recursive(4-1)) => 5 * 4 * ( 3 *recursive(3-1)) =>5 * 4 * 3 * ( 2 * recursive(2-1) =>5 * ( 4 * (3 * (2 * (1))))) =>120 This is called recursive callings... Edit : on num == 1, returning 2 cause final answer 240.
11th Sep 2020, 2:08 PM
Jayakrishna 🇮🇳
+ 1
If you want to calculate the factorial(n*(n-1)*(n-2)*...*1), i think you must return 1 instead of 2.
11th Sep 2020, 3:27 PM
Nathan Sikati
Nathan Sikati - avatar
+ 1
Oh, yes.. 😅🐍🐍😆 Thanks for pointing.. I did not see that returning 2.. See I write 1 only. I just concentrated on explanation that what Suparna Podder asked for.. So that why also, cause wrong calculation of 125 instead 120.. Yes, final answer we get is 240. Thank again... [I will edit..]
11th Sep 2020, 6:54 PM
Jayakrishna 🇮🇳