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;
}

Seems to me that you have a bunch of rouge braces.
see commented line below. There is an extra brace between each function!
I'm surprised that it compiles this way. Perhaps this is what is confusing the compiler?


#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;
   }
}  // This brace serves no purpose

void storeNum( int result[], int n )

Seems to me that you have a bunch of rouge braces.
see commented line below. There is an extra brace between each function!
I'm surprised that it compiles this way. Perhaps this is what is confusing the compiler?

look at the last paren of the for loop thats where it starts that paren is needed

OOPs ! I just saw that was part od the for statement.
Your style is a bit inconsistant.
Either you have opening braces at the END of a function line OR on lthe following line by itself (my preference) Then opening and closing braces can line up.
Each nested pair is indented from the previous.

I find that easier to follow.

something is wrong here:
Why do 99 passes on a single digit?
You only want 5,4,3,2,1
so i would be =n , no?
what happens now is:
5 %10 =0
and
5/10 =.5
no mattter how many times you do it!

void storeNum( int result[], int n )
{
   for ( int i = 99; i > 98; i-- ){
      result[i] = n % 10;
      result[i -1] = n / 10;
   }

Y'know, now that I actually took a hard look at the program, it is way more complicated than it needs to be.

If just getting an answer for n! is the aim, there is no need for the arrays or modulo, etc.
You don't need to include <cmath>
A simple loop to do the multiplication is all that is needed.

1)Decrement any n in a loop
2)multiply n by n-1
3) accumulate the result in x
4) continue x=x*(n-1)
5) test for (n-1) = 0
6) return x

Perhaps I missed the point here, but you did say that you were looking for a n! value?
I don't understand the significance of 98 iterations. Why store all those numbers in arrays? You are using allot of resources for no reason that I can determine.

Keep it SIMPLE!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.