In: Computer Science
Write a factorial C++ program where n=10,000,000
Save the following program in file factorial.cpp
#include<iostream>
using namespace std;
#define MAXSIZE 500000 // size of the array to hold intermidiate
multiplication results
int mult(int num, int res[], int res_size);
// Function to find factorial
void findFfactorial(int num) {
int res[MAXSIZE];
res[0] = 1;
int res_size = 1;
for (int i=2; i<=num; i++)
res_size = mult(i, res, res_size);
cout << "Factorial of the number is: ";
for (int i=res_size-1; i>=0; i--)
cout << res[i];
}
// function to find result of progressive multiplications in
each step
int mult(int num, int res[], int res_size) {
int carry = 0;
for (int i=0; i<res_size; i++) {
int prod = res[i] * num + carry;
res[i] = prod % 10;
carry = prod/10;
}
while (carry){
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
int main() {
//findFfactorial(10000000);
findFfactorial(10);
return 0;
}
The factorial of supplied number 10000000 will give very long
output,, which is not possible to show here. hence I ran the
program with small sample value 10, to show you the result
here.
Output:
Factorial of the number is: 3628800
You can run the program in your local machine to see the output.