C++ | Calculating factorial of number using while loop


This is a tutorial of c++ program to calculate the factorial of the number using while loop. The program calculates the factorial by multiplying the factorial by increased number. The program is not extendable. Go enjoy the program. Lets begin….

What’s a factorial?

You may have studied factorial in your maths book. The factorial is usually denoted by n! .i.e 5! is factorial of 5. The factorial is calculated as:- 5! =1x2x3x4x5.

Program to calculate the factorial of number using while loop

void main()
//clear the screen.
//declare variable type int
int no,fact=1,i;
//Input the two numbers save it
cout<<"Enter the no."<<endl;
//Calculate the factorial using loop
//show the output 'fact'
cout<<"The factorial is "<<fact;
//get character


Enter the no.
The factorial is 120

How does it work

  1. You enter the number.
  2. The factorial is calculated using while loop
  3. The factorial is printed.

Extending it

The program cannot be extended.


  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int (name them as you want.)
  5. Add the cout and cin of number.
  6. Add the while loop to calculate the factorial
  7. Print the factorial of the number.

At the end

You learnt creating the c++ program of Calculating the factorial of the given number. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Join India's Largest Code Learning Community on Uphub.io Get it on Google Play
Sharing options: