factorial (!)

how can i create the formula to calculate n!
Have you tried anything yet?
1
2
3
4
5
6
7
cin>>n
answer = 1
do
{
answer *=n
n-=1
}while(n>1)


Something like this maybe??
1
2
3
4
5
int fact(int n)
{
  if (n <= 0) return 1;
  return n * fact(n-1);
}
Last edited on
int main()
{
int i, n;
int fact=1;
cout<<"Enter the Value of n:";
cin>>n;
for(i=n ; i>=1 ; i--)
fact = fact*i;
cout<<Factorial: "<<fact;
return 0;
}
Topic archived. No new replies allowed.