Reputation: 2337
int main (void)
{
int i;
for (i=1; i<=20; i++) {
int j;
unsigned long long fac = 1;
for ( j = 1; j<=i; ++j) {
fac *= j;
}
printf ("%2i! = %ld\n", i, fac);
}
return 0;
}
From 14! to 20! outputs wrong values.
It either gives a negative number or the number is not big enough.. what is the problem?
Upvotes: 2
Views: 647
Reputation: 588
You are calculating correctly but printing incorrectly! use %llu :
printf ("%2i! = %llu\n", i, fac);
20! = 2432902008176640000
There!
Upvotes: 2
Reputation: 6548
The variable fac
is of type unsigned long long
. The correct format specifier for this type is is %llu
:
printf ("%2i! = %llu\n", i, fac);
Upvotes: 2
Reputation: 22114
printf ("%2i! = %ld\n", i, fac);
The problem is here, use proper notation for unsigned long long type value
Try:
printf ("%2i! = %llu\n", i, fac);
Upvotes: 8