#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Program to compute factorials...


    I need to write a program to compute factorials and store the answer in an array. It needs to handle large factorials. This is the way we were told to do this. I know I have something horribly wrong because all factorials = 0. ;)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int ansArr[3000];
    int total, rem=0, count;
    int i;
    
    int main(void)
    {
      int num;
      printf("For what factorial would you like to know the value? \n");
      scanf("%i", &num);
    
      i = 2999;
      ansArr[2999] = 1;
    
      for(count=2; count<=num; count++)
      {
        while(i>0)
          {
            total = ansArr[i] * count + rem;
            rem = 0;
               if(total>9)
               {
                 ansArr[i] = total%10;
                 rem = total/10;
               }
               else
                 ansArr[i] = total;
            i--;
          }
      }
    printf("%i! = %i\n", num, total);
    
    return 0;
    }
    Can someone point me in the correct direction?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Done.


    Done! ^_^
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    hey

    ok well im also new to c but ive recently done a factorial function.

    what we did was:

    Code:
    double total = 1;
    	int max = num + 1;
    	int x;
    	for ( x = 2; x < max; x++ )
    	{
    		total = total*x;
    	}
    and i think u should have %d for ur scanf
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    The %i format is valid for this problem. The big trouble was forgetting to provide output from ansArr .
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo