Here you will get program to find factorial of large number in C and C++.

Factorial of big numbers contain so many digits. For example factorial of 100 has almost 158 digits. So there is no data type available to store such a long value. But we can find factorial for large numbers using simple multiplication method that we used in our school time. Below I have shared the program for it.

## Program for Factorial of Large Number in C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include<stdio.h> int multiply(int x,int a[],int size) { int carry=0,i,p; for(i=0;i<size;++i) { p=a[i]*x+carry; a[i]=p%10; carry=p/10; } while(carry!=0) { a[size]=carry%10; carry=carry/10; size++; } return size; } int main() { int n,a[1000],i,size=1; a[0]=1; printf("Enter any large number:"); scanf("%d",&n); for(i=2;i<=n;++i) { size=multiply(i,a,size); } for(i=size-1;i>=0;--i) { printf("%d",a[i]); } return 0; } |

## Program for Factorial of Large Number in C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include<iostream> using namespace std; int multiply(int x,int a[],int size) { int carry=0,i,p; for(i=0;i<size;++i) { p=a[i]*x+carry; a[i]=p%10; carry=p/10; } while(carry!=0) { a[size]=carry%10; carry=carry/10; size++; } return size; } int main() { int n,a[1000],i,size=1; a[0]=1; cout<<"Enter any large number:"; cin>>n; for(i=2;i<=n;++i) { size=multiply(i,a,size); } for(i=size-1;i>=0;--i) { cout<<a[i]; } return 0; } |

**Output**

If you have any doubt regarding above program then you can ask it by commenting below.

Pritam SharmaNice Job.;-) 😉

Jadranko….you have to enlarge array a[ .. ] from 1000 to say 10000 elements – then it’s calculating corectly.

Adminyes it will not work for very big numbers like 500!, for that you have to increase the size of array a[]. It is working fine for 100!.

FeindflugJust take a std::vector and enlarge it dynamically. 🙂

Nathaniel ClelandThis seems to be a rather space inefficient implementation of bigint. Better would be to store an array of bits and work in base 2, which would be optimised by storing an array of 32 bit integers, storing the result of multiplication as a 64 bit integer to avoid data loss, and adding the overflow to the next column.

HashbangThis is a very nice and popular algorithm to calculate factorial of big number.

But there is a bug with your usage of memory (like other examples in other sites);

There is a waste of memory using an array like a[1000] if I want to calculate “small” number,

but even if I digit too big numbers, the array will be insufficient and program will crash.

One solution is use something like this:

int *a;

float stirling = sqrt(2*pi*n)*((n/e)^n);

approx = (int)stirling;

a = malloc(approx * sizeof(int));

where the variable “stirling” contain an approximated size of result stored into the array a[].

ZeroPlyHaskell snob here, lol… here’s the program in Haskell:

myFactorial :: Integer -> Integer

myFactorial n = foldl (*) 1 [1..n]

I tried 100000! and it spat out the 456,574 digit output in under 11 seconds. How do you expand your program to allow 100,000 as input, without worrying about overflows?