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?
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);
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);
You are calculating correctly but printing incorrectly! use %llu :
printf ("%2i! = %llu\n", i, fac);
20! = 2432902008176640000
There!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.