C++ program to find the factorial of the number
In this chapter of C++ program tutorial out task is:

Write a C++ program to find the factorial of a number
C++ program to find the factorial of a number
Below is the C++ program to find factorial of a number.
#include<iostream>
using namespace std;
void factorial(int n){
int fact = 1,i;
for(i=1;i<=n;i++){
fact = fact * i;
}
cout<<"The factorial of "<<n<<" is "<<fact;
}
int main()
{
int n;
cout<<"Enter the number to calculate the factorial\n";
cin>>n;
factorial(n);
return 0;
}
OUTPUT :
Enter the number to calculate the factorial
5
The factorial of 5 is 120
EXPLANATION :
In the above program:

The execution starts from the main function (here int main()).

A 4bit memory space is allocated to the integer n.

Then the integer "n" is taken as the input

Then the fuction factorial is called by passing the value of "n".

The facorial function is calculated and the required output is displayed.

The program then terminates in the line "return 0" returning a 0 value.
WORKING :
The working is for the factorial function.
let n = 5
for(i=1;i<=5;i++)
1st iteration:
fact = fact * i // fact =1 * 1 = 1 and i=2
2nd iteration:
fact = fact * i // fact =1 * 2 = 2 and i=3
3rd iteration:
fact = fact * i // fact =2 * 3 = 6 and i=4
4th iteration:
fact = fact * i // fact =6 * 4 = 24 and i=5
5th iteration:
fact = fact * i // fact =24 * 5 = 120 and i=6
And after this i=6 which is a false condition and the loop terminates
You can see the above code execution and output in codeblocks IDE:
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Sayantan Bose
 š Iām currently working on DS Algo skills
 š± Iām currently learning web develeopement
 šÆ Iām looking to collaborate with Oppia
 š« Reach me at https://www.linkedin.com/in/sayantanbose14134a1a6/
 š Pronouns: his/him
Page Views :
Published Date :
Aug 23,2020