Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. // C++ program to compute factorial of big numbers
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. // Maximum number of digits in output
  6. #define MAX 500
  7.  
  8. int multiply(int x, int res[], int res_size);
  9.  
  10. // This function finds factorial of large numbers
  11. // and prints them
  12. void factorial(int n)
  13. {
  14. int res[MAX];
  15.  
  16. // Initialize result
  17. res[0] = 1;
  18. int res_size = 1;
  19.  
  20. // Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
  21. for (int x=2; x<=n; x++)
  22. res_size = multiply(x, res, res_size);
  23.  
  24. cout << "Factorial of given number is \n";
  25. for (int i=res_size-1; i>=0; i--)
  26. cout << res[i];
  27. }
  28.  
  29. // This function multiplies x with the number
  30. // represented by res[].
  31. // res_size is size of res[] or number of digits in the
  32. // number represented by res[]. This function uses simple
  33. // school mathematics for multiplication.
  34. // This function may value of res_size and returns the
  35. // new value of res_size
  36. int multiply(int x, int res[], int res_size)
  37. {
  38. int carry = 0; // Initialize carry
  39.  
  40. // One by one multiply n with individual digits of res[]
  41. for (int i=0; i<res_size; i++)
  42. {
  43. int prod = res[i] * x + carry;
  44.  
  45. // Store last digit of 'prod' in res[]
  46. res[i] = prod % 10;
  47.  
  48. // Put rest in carry
  49. carry = prod/10;
  50. }
  51.  
  52. // Put carry in res and increase result size
  53. while (carry)
  54. {
  55. res[res_size] = carry%10;
  56. carry = carry/10;
  57. res_size++;
  58. }
  59. return res_size;
  60. }
  61.  
  62. // Driver program
  63. int main()
  64. {
  65. factorial(100);
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement