write a program to find out factorial of given number.



We know that factorial means if we input number 5 then we have to calculate this number like 1*2*3*4*5 and then the final calculation will be the factorial. The code is as follows:

#include<iostream.h>
#include<conio.h>
void main()
{
long n,i,fact=1;
clrscr();
cout <<"Enter the number=";
cin>>n;
i=1;
while(i<=n)
{
fact=fact*i;
i++;
}
cout <<"Factorial is="<<fact;
getch();
}

In the above program we have used the following header files as follows:
#include<iostream.h> used for input/output of data
#include<conio.h> provides several functions like clrscr(), getch() etc.
After that in main() function we have declared a variables n, I, fact=1. after that we have used clrscr(); function to clear the scree when program will run. It will clear the previous output from any program. Now we have used the cout function that shows message “Enter the number :”; and when we enter any number then this number is read by cin function and assign to n variable. After that we have initialized the value of variable I by 1. Now we have used while loop. Here we used condition i<=n means this loop will be continue till the ith value will not be equal to the entered number. Inside the loop, we have used fact*I and it is assigned to fact after that we have incremented the value of variable I by 1. This loop will be continued till the condition will not become false.
The output is as follows:

c++ program