I'm am trying to write a program that will do the factorail of big numbers and yet i still can not figure out why it does not work. when i do the factorail of 5 i get 20 it should be 120 and i cant figure out where the bug is?
#include <iostream>
#include <cmath>
using namespace std;
void addArrays(int array1[], int array2[],int result[])
{
int sum;
int carry = 0;
for (int i = 99; i >= 0; i--){
int answer = array1[i] + array2[i] + carry;
sum = answer % 10;
result[i] = sum + carry;
carry = answer / 10;
}
}
void storeNum( int result[], int n )
{
for ( int i = 99; i > 98; i-- ){
result[i] = n % 10;
result[i -1] = n / 10;
}
}
int multiplyArray(int array1[],int array2[],int result[], int n)
{
int fact1;
int fact2;
int sum1;
int sum2;
int carry1 = 0;
int carry2 = 0;
for (int j = 99; j >= 0 ; j--)
{
fact1 = n % 10;
fact2 = n / 10;
sum1 = ((fact1 * result[j]) + carry1) % 10;
array1[j] = sum1;
carry1 = ((fact1 * result[j]) + carry1) / 10;
sum2 = ((fact2 * result[j]) + carry2) % 10;
array2[j -1] = sum2;
carry2 = ((fact2 * result[j]) + carry2) / 10;
addArrays(array1, array2, result);
n--;
}
}
int main( ) {
int n;
int array1[100] = {0};
int array2[100] = {0};
int result[100] = {0};
int answer;
cin >> n;
while (!cin.eof() && n > 0){
storeNum(result, n);
multiplyArray(array1, array2, result, (n - 1));
cout <<"The value of " << n << "! is " << endl;
for (int i = 0; i <= 99; i++ )
{
cout << result[i];
}
cout << endl;
cin>> n;
}
return 0;
}