C++ Program to Find Factoial 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. }

advertisement
$ 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
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn