# C++ | Calculating factorial of number using while loop

Introduction

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

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int no,fact=1,i;
//Input the two numbers save it
cout<<“Enter the no.”<<endl;
cin>>no;
//Calculate the factorial using loop
i=1;
while(i<=no)
{
fact=fact*i;
i++;
}
//show the output ‘fact’
cout<<“The factorial is “<<fact;
//get character
getch();
}

Output

Enter the no.
5
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.

Explanation.

1. Include ‘iostream.h’ and ‘conio.h’ files.
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.