C++ Program to find the factorial of a number.

In this program, we see a simple way to calculate the factorial of a number.

In this program,

  • We use a for loop for multiplying all the numbers from 1 to ‘num‘.
  • We use 3 variables: num (for storing the number), factorial (to store the factorial of num) and i (to count from 1 to num).
  • We initialize variable factorial to 1 and multiply it with i in the for loop.
  • So, the for loop runs till i becomes equal to num, thus multiplying each number(in the form of i till num) with variable factorial gives us our factorial of the number in the variable factorial.

Example: num=3, factorial=1, i=1

Loop number:

  1. factorial=factorial*i  i.e., factorial=1*1=1.
  2. factorial=factorial*i  i.e., factorial=1*2=2.
  3. factorial=factorial*i  i.e., factorial=2*3=6.

Therefore, factorial of 3=6

Tested in: Visual C++/Visual Studio

The following two tabs change content below.

Lalit Mali

Lalit is a technology enthusiast, a programming lover and currently an Android fan.
  • Ajax tutorials

    I think its a better tutorial code.

  • preetysweety

    hi dude! i am basic learner of c++ and read ur c++ programming for factorial.can u pls explain why we need to use “using namespace std “and wat that actually means?

    inside for loop u have used “fact=fact*i”;
    will it work without defining fact as some function???

    waiting for ur reply dude..
    clear my doubts and help me to learn :)

  • doak

    @preetysweety,
    there was a little error in the program and i’ve fixed it, and also added description at the top. See if u can understand now.

    And fact is not a function, it was suppose to be a variable. It is factorial now.

    And ‘using namespace std’ tells the compiler that we will be using standard C++ library functions and objects.

    Alternative to ‘using namespace std’ in this program, use ‘std::’ before all cout, cin and endl and the program will work exactly the same.

  • RAMAN GUPTA

    #include
    #include
    void main()
    {
    int n,fact;
    int rec(int); clrscr();
    cout<”;
    cin>>n;
    fact=rec(n);
    cout<<endl<<"Factorial Result are:: "<<fact<<endl;
    getch();
    }
    rec(int x)
    {
    int f;
    if(x==1)
    return(x);
    else
    {
    f=x*rec(x-1);
    return(f);
    }
    }

  • talha

    i am a basic learner of c++ language.. tell me one thing plx abt this program plz that why we cant initialise or declare an another variable instead of factorial means that factorial=factorial*i so for this statment why we cant use separate variables for this statement ?? means factorial=factorial so why we cant declare another variable ? why 2 times factorial ???

  • talha

    i am wating for your reply

  • doak

    Talha, you can definitely use another variable if you wish. But that probably won’t make the program any better.

    Maybe You mean to do this:
    ——————–
    int product;
    for(int i=1;i<=num;i++)
    {
    product = factorial * i;
    factorial=product;
    }
    ———————
    But this is just making our code more lengthy. Although you can definitely use it if you feel more comfortable with this.

    If this was not what you meant in the question then feel free to ask it again.

  • deepa

    its simple…. easy to understand:)

  • arayaashraff

    hey i want to print factorials of numbers from 1 to 13 , my program printing right answers till 12 after it it give wrong answers
    heres my program can any1 please fix this error..
    #include
    void main()
    {
    long fact=1;
    //loop is started //
    for(int i=1; i<=20;i++)
    {
    fact=1;
    for(int j=1; j<=i);
    {
    fact=fact*j;
    j++;
    }
    cout<<i<<"! : "<<fact<<endl;
    }
    }

  • doak

    Hey Arayaashraff, the logic for your program is almost correct except for 1 error in syntax and 1 in the outer loop’s condition

    This is how your program should be:

    void main()
    {
    long fact=1;
    //Start the loop
    for(int i=1; i<=13; i++)
    {
    fact=1;
    for(int j=1; j<=i; j++)
    {
    fact=fact*j;
    }
    cout< }
    }

    And I can’t guess how u got the output from 1 to 12.
    The syntax error shouldn’t let your program run at all.

    Maybe you ran a previously compiled file?

    Anyways let us know if your still unable to run your program.

  • Abhi

    #include
    using namespace std;
    int main()

    {
    int a,f=1;
    cout <> a;
    for (int i=1;i<=a;i++)
    {
    f=f*i;
    }
    cud u tell me wat is wrong..for all no.s its giving output 2…

    cout << f;
    }

  • Abhi

    #include
    using namespace std;
    int main()

    {
    int a,f=1;
    cout <> a;
    for (int i=1;i<=a;i++)
    {
    f=f*i;
    }

    cout << f;
    typo error…this is the correct one

  • Abhi

    Hey wait a second..its removing my cout and cin statements?? wth? there’s no problem in te cin and cout statements, but still it always comes out to be two..

  • ramya

    program is very easy to grasp …very simple program,…

  • domy

    my program is just giving factorial for 1 and 2…..why it is giving wrong output for 3 and above numbers?…here is the code
    #include
    using namespace std;
    int main()
    {

    int n,i,j;
    int fact=1;
    cin>>n;
    for(i=1;i>i;
    for(j=1;j<=i;j++)
    {
    fact=fact*j;
    }
    cout<<fact<<endl;
    }

    system ("pause");
    return 0;
    }

  • domy

    my program is just giving factorial for 1 and 2…..why it is giving wrong output for 3 and above numbers?…here is the code
    #include
    using namespace std;
    int main()
    {
    int n,i,j;
    int fact=1;
    cin>>n;
    for(i=1;i>i;
    for(j=1;j<=i;j++)
    {
    fact=fact*j;
    }
    cout<<fact<<endl;
    }
    system ("pause");
    return 0;
    }

  • Jinson

    #include
    #include
    void main()
    {
    clrscr();
    int num;
    cout<<" Factorial Program"<<endl;
    cout<<endl;
    cout<>num;
    int fact=1;
    for (int i=1;i<=num;i++)
    {
    fact=fact*i;
    }

    cout<<"Factorial of "<<+num<<" is :"<<fact;
    getch();

    }

  • doak

    Jinson if you’re looking to to spot the error in your program, then accept input for num. Add a cin>>num statement somewhere before you try to use it.

    In your current program you’ve given no value to the num variable and you’re directly using it. That will result in an error.

  • erum

    I need a C++ progarm to find the factorial of a nimber using recursive functions.. can anyone plzz help..

  • Sean

    Please tel me how to create a header file so that we can bring factorial of a number directly from tat header file.,???

  • me gusta

    when i run my code with 20!, it outputs -2102132736… it should not be negative. is something wrong or do i need to include syntax that will allow me to output large numbers?? plz help!!

  • doak

    @me gusta
    Nothing is wrong here, it’s just the limit for an integer type variable.
    Make sure to never try to cross datatype limits.

    If u want to use larger numbers, u can try double but that will output the number in exponential.
    You can also put an if…else check statement to avoid input of numbers larger than 16.

  • fahad bangash

    some one plzz tell me how to find a factorial of a number using function……….m waiting for the reply

  • sasha

    hey can you send me the coding to get the following output :

    1!+1/2!+1/3!…..

    waiting for your reply !

    • Jagruti Chaugule

      The solution to your problem is given below:
      Note: The solution is in C# you can use the same logic and just change the print statement i.e. cout instead of Console.WriteLine

      output: 1! + 1/2! + 1/3! + 1/4! + 1/5! + 1/6! + 1/7! + 1/8! + 1/9!
      using System;
      public class Class1
      {
      static void Main(string[] args)
      {
      int s=1;
      for (int i = 1; i <= 9; i++)
      {
      if (i == 1)
      {
      Console.Write(“” + s + “! + “);
      }
      else if ((i != 1) && (i < 9))
      {
      Console.Write(“” + s + “/” + (i) + “! + “);
      }
      else
      {
      Console.Write(“” + s + “/” + (i) + “!”);
      }
      }
      Console.ReadKey();
      }
      }

  • wasi

    thankssssss allot

  • nancy

    #include
    #include
    #include

    using namespace std;

    int main()
    {
    int n;
    int factorial,i;

    cout <> n;

    for (i = 0; i <= n; i++)

    if (i == 0)
    factorial = 1;
    else
    factorial = factorial * i;

    cout << "The factorial of " << n << " is " << factorial << endl;
    system("PAUSE");
    }
    The outcome gives me like factorial of 4 gives 24,i want it to give me the factorial of 4= 4x3x2x1=24

  • Janani Swami

    Can yu provide me with the factorial of x/1!+(X)^2/2!-(x)^3+x^4/4!-……..(x)n/n