Write C++ Program to find factorial of Number.Today we will learn How to Find factorial of Number Using C++ Program.
But First of All lets discuss what is a Factorial. it is denoted by n!
What is a Factorial ?
it is denoted by n!. For example n=5 then factorial of 5 will be 1*2*3*4*5= 120. 5!= 120
|Code of++ Program to find factorial of Number|
Factorial program in C++ Logic:
- First think what is the factorial of a number?
- How mathematically it can be calculated?
- If you understand it now then it will be very easier to make a C++ Program logic to find the factorial.
- User enters a number and we have to multiply all numbers up-to entered number.
- Like if user enters 6 then Factorial should be equal to factorial= 1*2*3*4*5*6.
- In this case a for Loop will be very helpful. It will start from one and multiply all numbers up-to entered number after it loop will be terminated.
- Take a variable and initialized it to 1 and in loop store multiplication result into it like in below program a variable
- Factorial is used for this purpose.what is we does not initialized it to 1 and initialized it to zero or remain it uninitialized. In case of 0 our result will be zero in case of any number entered
- In case of not initializing it our answer will correct mostly but if variable contains garbage value then we will not be able to get correct result.
- It is recommended that to initialize it to one.
C++ Program to find factorial of Number
using namespace std;
cout<<" Enter Number To Find Its Factorial: ";
cout<<"Factorial of Given Number is ="<<factorial<<endl;
Also Read: How to write C++ Program to find factorial of Number
If you have any queries related to How to write C++ program to find factorial of Number please comment below: