September 20, 2024

# Program to Calculate Factorial of n Number

This article describes how to calculate factorial of n number in C/C++?. Let us describes about factorial number.

What is Factorial!
The factorial means to multiply a series of descending natural numbers. Its symbol is! And pronounced n factorial. But some people even say “4 shriek” or “4 bang”
In mathematics the factorial of a natural number n is the product of the positive integers less than or equal to n. It is written as n!

Examples:

0! = 1
1! = 1
2! = 2×1 = 2
3! = 3×2×1 = 6
4! = 4×3×2×1 = 24
5! = 5×4×3×2×1 = 120

Where is Factorial Used?
Factorials are used in many areas of mathematics. It is used particularly in Combinations and Permutations.

Can we have factorials for numbers like 0.6 or -5.217?
Yes we can! But you need to get into a subject called the “Gamma Function”.

Program to Calculate Factorial of n Number (using for loop)

#include
#include
int main(){
clrscr();
long double n,i,fact=1;

cout<<” ” This program will claculate Factorial of n number (using For loop). “”<<endl<<endl;

xx:
cout<<“How many numbers ?. Give a integer number.”<<endl;
cin>>n;

if (n<0) {
cout<<“Negative number not allow, Please give integer number.”<<endl;
goto xx;
}

for(i=1;i<=n;i++)
fact=fact*i;

cout<<endl;
cout<<“Factorial of “<<n<<” is : ” <<fact;

getch();
}

Program to Calculate Factorial of n Number (using do-while loop)
#include
#include
int main(){
clrscr();
long double  n,i,fact=1;

cout<<”  ” This program will show the Factorial of n numer (usin do-while loop). “”<<endl<<endl;

xx:
cout<<“Give how many numbers ?. Give a integer number.”<<endl;
cin>>n;

if (n<0) {
cout<<“Negative number is not allow, Please give integer number.”<<endl;
goto xx;
}

i=1;
do {
fact=fact*i;
i++;
}

while(i<=n);

cout<<endl;
cout<<“Factorial of “<<n<<” is : ” <<fact;

getch();
}

Program to Calculate Factorial of n Number (using while loop)
#include
#include
int main(){
clrscr();
long double n,i,fact=1;

cout<<” ” This program will show the Factorial of n number (using while loop).””<<endl<<endl;

xx:
cout<<“How many numbers ?.Give a integer number. “<<endl;
cin>>n;

if (n<0) {
cout<<“Negative number is not allow, Please give integer number.”<<endl;
goto xx;
}

i=1;
while(i<=n){
fact=fact*i;
i++;
}

cout<<endl;
cout<<“Factorial of “<<n<<” is : ” <<fact;

getch();
}

#### Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →