To know how to find factorial of number in C++ program, you need to know about c++ looping.
C++ Program to Find Factorial of Number
/*C++ Program to Find Factorial of Number*/
#include<iostream>
using namespace std;
int main()
{
int i,n,t;
cout<<"Enter a number : ";
cin>>n;
t=n;
for(i=n-1;i>0;i--)
{
n=n*i;
}
cout<<endl;
cout<<"Factorial of "<<t<<" is "<<n;
return 0;
}
OUTPUT:
Enter a number : 10
Factorial of 10 is 3628800
EXPLANATION:
First we read a number from keyboard using cin>>n statement. Then, we assign this number to variable t. In for loop, we assign (n-1) to variable i, always increment to variable i by 1. And condition is i>0. After the for loop, we display the factorial using cout statement.
0 comments:
Post a Comment