Cpp program to find factorial of a number
- Home
- Calculations
- Cpp program to find factorial of a number
- On
- By
- 0 Comment
- Categories: Calculations
Cpp program to find factorial of a number
Cpp program to find factorial of a number
In this tutorial, we will discuss the concept of Cpp program to find factorial of a number
Factorial is a product of all positive descending integer begins with a specified number (n) and calculates upto one
There are many ways to calculate factorial using C++ language. some of them are described as below.
Example
factorial of 5 is
5!=5*4*3*2*1=120
factorial of 4 is
4!=4*3*2*1=24
factorial of n is
n!=n*(n-1)*....2*1
Cpp program to find factorial
Find factorial of a number using for loop
#include <iostream> #include <conio.h> using namespace std; int main() { int num,factorial=1; cout<<"Enter number for find factorial: "; cin>>num; for(int i=1; i<=num; i++){ factorial*=i; //factorial=factorial*i } cout << "factorial of "<<num<<" is: "<<factorial<< endl; getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter number for find factorial: 6 Factorial of 6 is:720
This program intakes input (a positive integer) from the user and calculates the factorial of the given number using For loop and displays the result.
Find factorial of a number using while loop
#include <iostream> #include <conio.h> using namespace std; int main() { int num,factorial=1; cout<<"Enter number for find factorial: "; cin>>num; int i=1; while(i<=num){ factorial*=i; //factorial=factorial*i i++; } cout << "factorial of "<<num<<" is: "<<factorial<< endl; getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter number for find factorial: 7 Factorial of 6 is:5040
This program intakes input (a positive integer) from the user and calculates the factorial of the given number using while loop and displays the result.
Find factorial of a number using the do-while loop
#include <iostream> #include <conio.h> using namespace std; int main() { int num,factorial=1; cout<<"Enter number for find factorial: "; cin>>num; int i=1; do{ factorial*=i; //factorial=factorial*i i++; } while(i<=num); cout << "factorial of "<<num<<" is: "<<factorial<< endl; getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter number for find factorial:8 factorial of 8 is: 40320
This program intakes input (a positive integer) from the user and calculates the factorial of the given number using Do-while loop and displays the result.
Find factorial of a number using C++ recursion
#include <iostream> #include <conio.h> using namespace std; int factorial(int n); int main() { int num; cout<<"Enter number for find factorial: "; cin>>num; cout << "factorial of "<<num<<" is: "<<factorial(num)<< endl; getch(); return 0; } int factorial(int n) { if(n>1) return n*factorial(n-1); else return 1; }
When the above code is compiled and executed, it produces the following results
Enter number for find factorial: 6 factorial of 6:720
This program intakes input (positive integer) from the user and calculates the factorial of the given number using
recursion and displays the result.
Suggested for you
C++ do-while loop
Similar post
find factorial of a number using the function in C++
find factorial of a number using function in C