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 | #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 | #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
Enter any large number:250
3232856260909107732320814552024368470994843717673780666747942427112823747555111209488817915371028199450928507353189432926730931712808990822791030279071281921676527240189264733218041186261006832925365133678939089569935713530175040513178760077247933065402339006164825552248819436572586057399222641254832982204849137721776650641276858807153128978777672951913990844377478702589172973255150283241787320658188482062478582659808848825548800000000000000000000000000000000000000000000000000000000000000
If you have any doubt regarding above program then you can ask it by commenting below.