Recursion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Recursion

A function which can call by itself. long fact(int); main () { int n; long f; cout<<"Enter n"; cin>>n; f=fact (n); cout <<fact (n); return0; } long factorial(int n) { if (n==1||n==0) { return 1; } else { return n * factorial(n-1); } } //output: Enter n 5 fact (5) 120

20th May 2017, 1:54 PM
HARINI M
HARINI M - avatar
8 Answers
+ 3
@Setiawan I couldn't find anything on there describing that posting the code on the question is bad. Although, I'm not sure what the question is, which I guess was your point xD. 😜 @Harini You have 'long f' yet you never use it. You assign f to the value of factorial(n) (which is wrong because you wrote fact, when the function is named factorial. So the output is really compilation error) but never used the long. When you printed out you just called the factorial function again. Fix: f = factorial(n); cout<<f; Or don't use the long at all.
20th May 2017, 3:26 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
@Setiawan Ah, yea idk why they wouldn't use code playground. I'd assume it's just copy pasted then, from which they would not be learning from. 😶 Idk though.
21st May 2017, 2:43 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
@Rrestoring faith it was not question, he/she just post a code. why he/she didn't use codeplayground to test his/her code? did he/she seriously learning ?
21st May 2017, 2:39 AM
Setiawan Next
Setiawan Next - avatar
+ 1
c'mon why people post code on question. write them on code playground! https://www.sololearn.com/discuss/321242/?ref=app
20th May 2017, 2:01 PM
Setiawan Next
Setiawan Next - avatar
+ 1
@Setiawan I feel the same as you. I used to advise them use code playground. But many of them are just do not like to share it from code playground, maybe they ain't sure code can be share from there.
21st May 2017, 4:44 AM
Calviղ
Calviղ - avatar
+ 1
@Calvin Maybe we should give feedback to Sololearn to give faq page on how using Sololearn feature. So we don't have to explain everytime to many duplicated question.
21st May 2017, 4:52 AM
Setiawan Next
Setiawan Next - avatar
+ 1
@Setiawan We could try to inform SL. Another reason why they are not awake that the code can be inserted to post here is the function of "+ Insert..." selection is too vague.(in fact, it took me sometime to realise I can use it to insert code) Since it only contains 2 selections "Insert Code" and "Insert Post", why don't directly show these 2 selection beside the POST button instead?
21st May 2017, 5:04 AM
Calviղ
Calviղ - avatar
0
@Calvin i agree.
21st May 2017, 5:24 AM
Setiawan Next
Setiawan Next - avatar