0

I'm currently testing the maximum values of n! where n is a positive integer entered by the user. I tested this with:

  • short
  • int
  • long
  • long long
  • float
  • double

With all of the above types the program prints the correct value. However, when I declare factorial to be of type long double, the program behaves strangely. For example, I enter 10!, the program outputs -21340077980675728 with ~50 zeros after the 8, which is obviously not correct. So where's my mistake?

#include <stdio.h>

int main(void)
{
    int n, i;
    long double factorial = 1.0;

    printf("Enter a positive integer: ");
    scanf(" %d", &n);

    for (i = n; i >= 1; i--){
        factorial *= i;
    }
    printf("Factorial of %d: %.0Lf\n", n, factorial);
    return 0;
}
Shuster
  • 167
  • 5

0 Answers0