RK JOLLY
RK JOLLY

Reputation: 39

My code to calculate in factorial (using recursion) works up to 24 but shows incorrect answers after that in c. Please check

#include <stdio.h>
int main()
{
    int n, t, rem, i, j, k;
    scanf("%d", &t);
    int ans[t], integer[1000];
    for(i=0; i<t; i++)
    {
        int count=0;
        scanf("%d", &n);
        for(j=0; j<1000; j++)
        {
            integer[j]=0;
        }
        for(j=0, k=n; k>0; k/=10, j++)
        {
            integer[j]=k%10;
            count++;
        }
        factorial(n, count, integer);
    }
    return 0;
}
void factorial(int n, int count, int* integer)
{
    int i, j, k, rem=0, temp;
    if(n==1)
    {
        for(i=count-1; i>=0; i--)
        {
            printf("%d", integer[i]);
        }
        printf("\n");
        return;
    }
    else
    {
        for(i=0; i<count; i++)
        {
            temp=integer[i]*(n-1);
            integer[i]=(temp%10)+rem;
            rem=temp/10;
            if(i==count-1)
               {
                    if(rem!=0)
                    {
                        for(j=0, k=rem; k>0; k/=10, j++)
                        {
                        integer[count]=k%10;
                        count++;
                        }
                        break;
                    }
               }
        }
        factorial(n-1, count, integer);
    }

}

Explanation : I save the numbers in the inverse way ex input :100 integer saved in array : 0 0 1 0 0 0 0... then as factorial function is called it takes n=100, count=3, and the integer array as input. we multipy the first element of the array with n-1 and carry on the remainder... this carries on until the whole integer array is multiplied with 99, then we again call factorial thus multiplying the array with 98 and so on until we reach 1 where we ultimately print the answer.

Problem : the code gives correct result upto 24 only and gives wrong output thereafter.

Upvotes: 1

Views: 70

Answers (2)

user3629249
user3629249

Reputation: 16540

the calculation is overflowing the capability of an integer.

Upvotes: 1

bruno
bruno

Reputation: 32596

you suppose each element in integer is between 0 and 9 but this is not the case, adding a space after writing a digit indicates the problem, for instance computing fact from 1 up to 22 :

1 
2 
6 
2 4 
1 2 0 
7 2 0 
5 0 4 0 
4 0 3 2 0 
3 6 2 8 8 0 
3 6 2 8 8 0 0 
3 9 9 1 6 8 0 0 
4 7 8 10 0 1 6 0 0 <<< wrong value for !12
6 2 2 7 0 2 0 8 0 0 
8 7 1 7 8 2 9 1 2 0 0 
1 3 0 7 6 7 4 3 6 8 0 0 0 
2 0 9 2 2 7 8 9 8 8 8 0 0 0 
3 5 5 6 8 7 4 2 8 0 9 6 0 0 0 
6 4 0 2 3 7 3 7 0 5 7 2 8 0 0 0 
1 2 1 6 4 5 0 10 0 4 0 8 8 3 2 0 0 0  <<< wrong value for 19
2 4 3 2 9 0 2 0 0 8 1 7 6 6 4 0 0 0 0 
5 1 0 9 0 9 4 2 1 7 1 7 0 9 4 4 0 0 0 0 
1 1 2 3 10 0 0 7 2 7 7 7 7 6 0 7 6 8 0 0 0 0 <<< wrong value for 22

So your problem comes because you do not manage enough the carry

Example in 4 7 8 10 0 1 6 0 0 handling in a right way produces 4 7 9 0 0 1 6 0 0 as expected

To solve that in factorial after the line

rem=temp/10;

add

if (integer[i] > 9)
{
    rem += integer[i] / 10;
    integer[i] %= 10;
}

Out of that :

  • ans[t] is useless
  • when you use scanf or equivalent function check the result to be sure a valid value was enter
  • if the result use more that 1000 digit in base 10 you will write out of integer

Upvotes: 2

Related Questions