# Calculate factorial of a number in C++

Here, we will learn to find the factorial of a number in C++. We generally need to calculate factorial of numbers in mathematics, while finding permutations and combinations, algebra, etc. Here, we will see the method to find factorial of a number and implementation of this method using a C++ program. Also, we will see a C++ program to find the factorial using a recursive function.

## Factorial of a number

The factorial of a positive number say ‘n’ is calculated as –

factorial(n) = n * (n-1) * (n-2) * (n-3) * (n-4) * ……….. * 2 * 1           where n>1

The factorial is represented as ‘!’ symbol. Suppose we are finding the factorial of a number say ‘n’, then we will write it as ‘n!’ mathematically.

For example –
Factorial of 4 is 4*3*2*1 = 24

Remember, if the number is 0, then the factorial of that number will be 1 as per empty product convention i.e. 0! = 1.

## Calculating the factorial of a number in C++

So, let’s implement a C++ program to find the factorial of a number. In the program, we take the number by the user as an input. By using a loop we can calculate the factorial by multiplying all the positive numbers less than or equal to the number given by the user. So the C++ program to calculate the factorial of a number is –

```#include<iostream>
using namespace std;
int main()
{
int i,fact=1,number;
cout<<"ENTER A NUMBER : ";
cin>>number;
cout<<"FACTORIAL OF "<<number<<" IS : ";
for(i=1;i<=number;i++)
{
cout<<i;
if(i<number)
cout<<"*";
fact=fact*i;
}
cout<<"\nFACTORIAL OF " <<number<<" IS : "<<fact;
return 0;
}```

Here, we store the factorial of a number in an integer variable ‘fact’. The for loop iterates from 1 up to the number and the value is multiplied to the variable ‘fact’. Finally, the final result is the value in variable ‘fact’.

#### C++ program output

The program can calculate factorial of any positive number which the program takes from the user as input. It displays the factorial of that number after calculation. So the output of the above C++ program is –

```[email protected]:~/cpp\$ g++ fact.cpp
[email protected]:~/cpp\$ ./a.out
ENTER A NUMBER : 6
FACTORIAL OF 6 IS : 1*2*3*4*5*6
FACTORIAL OF 6 IS : 720
[email protected]:~/cpp\$```

## C++ program to calculate factorial using recursion

Also, we can calculate the factorial of a number using a recursive function. In recursive function, we call it within its function definition. So, the recursive function returns the factorial to the main calling function at the end. The C++ program for factorial by using a recursive function is –

```#include<iostream>
using namespace std;
int factorial(int);
int main()
{
int num,fact;
cout<<"ENTER A NUMBER : ";
cin>>num;
cout<<"FACTORIAL OF "<<num<<" IS : ";
fact=factorial(num);
cout<<"\nFACTORIAL OF "<<num<<" IS : "<<fact;
return 0;
}
int factorial(int n)
{
if(n==0)
return 1;
else
{
cout<<n;
if(n>1)
cout<<" * ";
return (n*factorial(n-1));
}
}```

#### The output of the C++ program for factorial using recursion

The output of the above program which uses a recursive function to calculate the factorial of a number is –

```[email protected]:~/cpp\$ g++ fact_rec.cpp
[email protected]:~/cpp\$ ./a.out
ENTER A NUMBER : 3
FACTORIAL OF 3 IS : 3 * 2 * 1
FACTORIAL OF 3 IS : 6
[email protected]:~/cpp\$```

Thank you for reading this tutorial. I hope this tutorial was useful for you.