Factorial function only counts to 12

Viewed 301

This factorial function starts giving wrong results with 13 and above. I have no idea why.

#include <stdio.h>

int fatorial (int p);

int main() {
    int x = 13;
    int test = fatorial(x);
    printf("%d", test);
}  

int fatorial (int p) {
    if (p <= 0)
        return 1;
    else
        return p*fatorial(p-1);
}

for x = 0, 1, 2 ...12 it prints the right result, but for 13! it prints 1932053504 which is not correct. For x=20 it prints -210213273 for example.

I know that this is not the best way to do a factorial. Its my homework tho, it HAS to be this way.

4 Answers

If you try this you will get the maximum value that int can hold:

#include <stdio.h>
#include <limits.h>
int main(void) 
{
    printf("%d\n", INT_MAX);
}

Your code causes overflow.

You could get a few more numbers if you use a bigger type, but not by very much. You could use this:

unsigned long long fatorial (unsigned long long p) {
    if (p <= 0)
        return 1;
    else
        return p*fatorial(p-1);
}

It won't get you far though. If you want bigger than that you need to find a library for bigger integers or create some custom solution. One such library is https://gmplib.org/ but that is likely out of scope for your homework.

And btw, a condition like p <= 0 is not good. It indicates that the factorial of a negative number is always one, which is false.

It is because after 12, the result of factorial of any number exceeds the size of int.

you can try the following code:

#include<stdio.h>
int main()
{
    int a[100],n,counter,temp,i;
    a[0]=1;
    counter=0;
    printf("Enter the number: ");
    scanf("%d",&n);
    for(; n>=2; n--)
    {
        temp=0;
        for(i=0; i<=counter; i++)
        {
            temp=(a[i]*n)+temp;
            a[i]=temp%10;
            temp=temp/10;
        }
        while(temp>0)
        {
            a[++counter]=temp%10;
            temp=temp/10;
        }
    }
    for(i=counter; i>=0; i--)
        printf("%d",a[i]);
    return 0;
}

The result of the function is too big. I think big int would work better for your purposes. Big int allows you to have bigger numbers. Also, this is what I would do.

int x = the number you want to factorialize
int ans = 1;
(Then instead of all of those functions)
for(var i = x; i > 0; i--) {
ans = ans*i;
}

System.out.println(ans);

Javascript link: https://jsfiddle.net/8gxyj913/

I need to get to 100!

100! is about 9.332622e+157. Simply using standard integer types is insufficient. 32-bit int is good to 12!. With 64-bit integer math, code could get to about 21!

Could use floating point math and give up precision.

Instead consider a string approach.

Related