Updated March 17, 2023
Introduction to Factorial Program in C++
We have all studied the factorial in mathematics, It is easy to calculate. But it becomes complex when we have to calculate the factorial for a large number. In this article, we are going to see how to calculate the factorial in the C++ language. Factorial is denoted by exclamation sign i.e.! sign. Factorial for a negative number does not exist. We can calculate factorial only for a positive number. Below are sample examples that show the calculation of factorial for numbers 0 to 10.
- Factorial of 0! = 1
- Factorial of 1! = 1
- Factorial of 2= 2 * 1 = 2
- Factorial of 3! = 3 * 2 * 1 = 6
- Factorial of 4! = 4 * 3 * 2 * 1 = 24
- Factorial of 5! = 5 * 4 * 3* 2 * 1 = 120
- Factorial of 6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
- Factorial of 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
- Factorial of 8! = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 40320
- Factorial of 9! = 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 362880
- Factorial of 10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3628800
How to Calculate Factorial in C++ Language by using the Various methods?
Here we are going to discuss how to calculate factorial in a C++ language using various methods like the if-else statement, for loop, recursion method, and function with the help of examples:
Example #1
Factorial program in C++ language by using if-else statement
Code:
#include<iostream>
using namespace std;
int main()
{
int num, i, fact_num = 1;
cout << "Enter the positive number to find the factorial: ";
cin >> num;
// if entered number is negative show the error
if (num< 0)
cout << "Error! You have entered negative number and Factorial for negative number does not exist.";
else
{
for(i = 1; i <= num; ++i)
{
fact_num*= i; // factorial = factorial*i;
}
cout << "Factorial of the entered number is " << fact_num;
}
return 0;
}
Explanation of the above code: In the above example, we have initialized three variables i.e. num, I and fact_num. Here to calculate the factorial we have used the if-else statement. If else statement is to check the condition before calculating the factorial for the entered number. If the number entered by the user is negative it throws an error that shows Error! You have entered a negative number and Factorial for negative numbers does not exist. If the entered number is positive if will transfer control to else statement where the condition for the calculation of factorial is stated.
Output:
Here the number entered by a user is negative hence it shows the error message.
Here the number entered by the user is positive hence it gives the output for factorial 5 i.e. 120
Example #2
Factorial program in C++ language by using the For loop
Code:
#include<iostream>
using namespace std;
int main()
{
int i, fact_num = 1, num;
cout << "Enter random number to find the factorial: ";
cin >> num;
for(i = 1; i <= num; i++)
{
fact_num = fact_num * i;
}
cout << "Factorial of the given number is " << fact_num;
return 0;
}
Explanation of the above program: Here to calculate the factorial, we have used for loop method. In for loop, we have mentioned some conditions to calculate the factorial. Here we have an initialized variable I with 1 that is the number it will take for i is 1. Then we have mentioned the condition that variable I should be equal to and less than number which is entered by the user. Then we have mentioned the increment condition to increase the value of variable I each time at the iteration process.
Output:
Here we have calculated the factorial for 10.
Example #3
Factorial program in C++ language by using recursion method
Code:
#include<iostream>
using namespace std;
int factorial(int num);
int main()
{
int num, fact_num;
cout << "Enter random number to find the factorial:";
cin >> num;
if(num < 0)
cout << "Negative integer factorial is not described." << endl;
else
{
fact_num = factorial(num);
cout << "Factorial of the given number is " << fact_num;
}
return 0;
}
int factorial(int num)
{
if (num == 0)
return 1;
else
return(num * factorial(num - 1));
}
Explanation of the above program: Here we have used the recursion method to calculate the factorial. The recursion method is quite similar to the function method. The recursion method is mostly used to solve the problem easily. Here we have declared factorial as a function and pass num as a parameter. In the factorial method, we have used the if-else statement to check whether the entered number is positive or not as we have used in the first example. Then we have called the factorial method where statements for factorial are executed.
Output:
Example #4
Factorial program in C++ language using the function
Code:
#include<iostream>
using namespace std;
int factorial(int n);
int main()
{
int num, fact_num = 1;
cout << "Enter random number to find the factorial: ";
cin >> num;
cout <<"Factorial of the given number is "<< factorial(num);
return 0;
}
int factorial(int n)
{
int count_num, result = 1;
for (count_num = 1; count_num <= n; count_num ++)
result = result * count_num;
return result;
}
Explanation of the above program: Here to calculate factorial for number we have used the function method. First, we create a factorial function and pass n as a parameter to store the number value. In main method, we have declared input to allow the user to enter a value and output to print the factorial. After that, we call a function factorial where the logic for the factorial is defined.
Output:
Conclusion
In this article, we have seen programs of how to calculate the factorial of a number in C++ language by using conditional statements, for loop, recursion method, and functions along with examples. I hope this article will help you in understanding the working of factorial in the C++ language.
Recommended Articles
This is a guide to Factorial Program in C++. Here we discuss how to calculate factorial in c++ in various methods. You can also go through our other related articles to learn more-