i need help with this pls

Adam2012 (22)
Write a recursive function Factorial. The function takes a number and returns the factorial of that number if its more than 0, or else return 1. Main will call this function up to 10 times to Show up to 10 !
The sample output as below.
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Adam2012 (22)
to let u know that i solved the question but i dont know how to call the function 10 times and get the sample output ???
Script Coder (352)
Do you know what recursion is?
Adam2012 (22)
not really but i think it is to make a function defies itself
Script Coder (352)
http://www.cprogramming.com/tutorial/lesson16.html
or if that is not helpful just google "recursion tutorial"
Adam2012 (22)
i could calculate the factorial for only one number but i couldnt get for the rest
meaning i couldnt get as the sample output above
should i use a loop or what???
Script Coder (352)
something to this effect:
1
2
3
4
5
6
int fact(int N) //Function that returns N!
cout<<"enter number"<<endl;
cin<<ans;
for(i=0; i<ans; i++) {
	cout<<i<<"! ="<<fact(i);
}

NOTE, this is my version of pseudocode .
Last edited on
Adam2012 (22)
now i got it thank you very much but if u dont mind can u tell me the secret ???
i mean the point so i can learn and apply for another questions???
i am thankful for u
Script Coder (352)
The secret to what? C++, recursion, programming, asking questions?
Adam2012 (22)
sorry for makin u confused,
i meant the idea behind using for loop???
why did u put <<fact(i);? while i doesnt exists in the function that return N??

Script Coder (352)
You should really read the tutorial on this site, it will help you a lot. But just a basic explanation or two:
a for loop is generally used to iterate over something, but it has other uses as well. here it is used to call the fact function with different values and print the results.
the line:
cout<<i<<"! ="<<fact(i);
cout is the standard output stream and the "<<" are used instead of:
cout(//stuff to output, more stuff, even more stuff)
but like i said read the tutorial:
http://www.cplusplus.com/doc/tutorial/
Adam2012 (22)
ok thanks a lot. i highly appreciate ur help
Topic archived. No new replies allowed.