C++ Program to Find Factorial of Large Numbers

This C++ Program demonstrates the the computation of Factorial of Large Numbers.

Here is source code of the C++ Program to Find Factorial of Large Numbers. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Find Factorial of Large Numbers
  3.  */
  4. #include <cstring>
  5. #include <iostream>
  6. #include <cstdlib>
  7. #define ll long long
  8. using namespace std;
  9.  
  10. int fact[101][200] = {0};
  11.  
  12. /* 
  13.  * Find Factorial of Large Numbers
  14.  * fact[i][0] is used to store the number of digits
  15.  */
  16. void fact_large(int n)
  17. {
  18.     int i;
  19.     fact[1][0] = 1;
  20.     fact[1][1] = 1;
  21.     if (fact[n][0] == 0) 
  22.     {
  23.         for (i = n - 1; i > 0 ; i--) 
  24.         {
  25.             if (fact[i][0] != 0)
  26.                 break;
  27.         }
  28.         for ( ; i < n; i++) 
  29.         {
  30.             int j = 1;
  31.             int carry = 0;
  32.             int len = fact[i][0];
  33.             while (len--)
  34.             {
  35.                 int temp = (i + 1) * fact[i][j] + carry;
  36.                 fact[i + 1][j] = temp % 10;
  37.                 carry = temp / 10;
  38.                 j++;
  39.             }
  40.             while (carry > 0) 
  41.             {
  42.                 fact[i + 1][j] = carry % 10;
  43.                 carry /= 10;
  44.                 j++;
  45.             }
  46.             fact[i + 1][0] = j - 1;
  47.         }
  48.     }
  49.     for (i = fact[n][0]; i > 0; i--) 
  50.     {
  51.         cout << fact[n][i];
  52.     }
  53.     cout<<endl;
  54. }
  55. /* 
  56.  * Main
  57.  */
  58. int main()
  59. {
  60.     int n;
  61.     while (1)
  62.     {
  63.         cout<<"Enter interger to compute factorial(0 to exit): ";
  64.         cin>>n;
  65.         if (n == 0)
  66.             break;
  67.         fact_large(n);
  68.     }
  69.     return 0;
  70. }

$ g++ fact_large.cpp
$ a.out
 
Enter interger to compute factorial(0 to exit): 100
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Enter interger to compute factorial(0 to exit): 50
30414093201713378043612608166064768844377641568960512000000000000
Enter interger to compute factorial(0 to exit): 72
61234458376886086861524070385274672740778091784697328983823014963978384987221689274204160000000000000000
Enter interger to compute factorial(0 to exit): 0
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.