C++ Program:

- To print factorial of a number

Example:

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;
}

Output:

  Enter your number : 3
  Factorial of 3 : 6
Program
number

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 !

Quick
Tutorial

Quote

Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.