# C++ Program To Find Factorial

Problem:- Write A C++ Program To Calculate Factorial Of A Given Number or factorial program in c or Factorial For Large Number Using Array.

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!. This fact was known at least as early as the 12th century, to Indian scholars.
Source:- Wikipedia

Logic:- Suppose we want to find factorial of number 6 then we can multiply number from 1 to 6 (n) like 1 * 2 * 3 * 4 * 5 * 6 = 720. So we can use a loop and run the loop from 1 to number n (the number whose factorial you want) and multiply a number, this is a simple way to find a factorial. You can also find the factorial program for Large Number.

Factorial Use's in Real Life:-

1. Trigonometry,
2. The exponential function
3. Permutations and Combinations
4. Calculus (Like Taylor’s Series and Maclaurin Series)
5. Data Distributions

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 = {0};
void fact_large(int n)
{
int i;
fact = 1;
fact = 1;
if (fact[n] == 0)
{
for (i = n - 1; i > 0 ; i--)
{
if (fact[i] != 0)
break;
}
for ( ; i < n; i++)
{
int j = 1;
int carry = 0;
int len = fact[i];
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] = j - 1;
}
}
for (i = fact[n]; 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

You May Also Like:-

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.