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
#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.
#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.
#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.
#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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…