- To print factorial of a number
User Input: 5
Output:
Factorial of 5 : 120
#include <iostream>
using namespace std;
int main() {
int num;
long long fact=1;
cout<<"Enter your number : ";
cin>>num;
cout<<"Factorial of "<<num<<" : ";
for(int i=1; i<=num; i++)
fact*=i;
cout<<fact;
return 0;
}
Enter your number : 3 Factorial of 3 : 6Program
You should know first :
Factorial of N : 1*2*3*4.....*N
Factorial of 0 : 1
The variable which holds factorial value (above fact
) must be of type long long
Because,
long long : Factorial of a number can be very large [valid for upto 20!]
for(int i=1; i<=num; i++)
fact*=i;
Concept
Coming Soon !
QuickIn order to succeed you must fail, so that you know what not to do the next time.