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
Similar post
find factorial of a number using the function in C++
find factorial of a number using function in C
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…