# C++ Program To Calculate Factorial Of A Given Number

Problem :- Write A C++ Program To Calculate Factorial Of A Given Number .

What Is Factorial :- In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5!=5*4*3*2*1=120.

The value of 0! is 1, according to the convention for an empty product *

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars .
source :- Wikipedia

See Also :- C Program To Calculate Factorial Of A Given Number

Solution :-

Method 1 :- Factorial Using For Loop

#include<iostream>
using namespace std;

int main()
{
Visit http://www.programmingwithbasics.com/
*/

unsigned long long int fact=1;
int i,num;

cout<<"Enter The Number. You Want Factorial  :";
cin>>num;

for(i=1;i<=num;i++)
{
fact=fact*i;
}

cout<<"\nFactorial of "<<num<<" Is = "<<fact<<endl;
return 0;

}

Method 2 :-  Factorial For Large Number Using Array

#include <cstring>

#include <iostream>
#include <cstdlib>
#define ll long long
using namespace std;
int fact[10001][10000] = {0};
void fact_large(int n)
{
int i;
fact[1][0] = 1;
fact[1][1] = 1;
if (fact[n][0] == 0)
{
for (i = n - 1; i > 0 ; i--)
{
if (fact[i][0] != 0)
break;
}
for ( ; i < n; i++)
{
int j = 1;
int carry = 0;
int len = fact[i][0];
while (len--)
{
int temp = (i + 1) * fact[i][j] + carry;
fact[i + 1][j] = temp % 10;
carry = temp / 10;
j++;
}
while (carry > 0)
{
fact[i + 1][j] = carry % 10;
carry /= 10;
j++;
}
fact[i + 1][0] = j - 1;
}
}
for (i = fact[n][0]; i > 0; i--)
{
cout << fact[n][i];
}
cout<<endl;
}

int main()
{
int n,t;
cout<<"Enter The Number \n";
cin>>n;
fact_large(n);
return 0;
}

Output :-

1. Factorial Using For Loop

2.Factorial For Large Number Using Array