Click Here to see the problem detail.
Solution
To ignore all the non-sense, if the input is between 8 to 13, print n!. It’s always overflow if n >= 14, and always underflow from 0 to 7. For negative numbers, negative odd numbers are overflows and negative even numbers are underflows.
Source Code
#include<stdio.h>
void factorial(long int n)
{
long int fact=1;
while(n>0)
{
fact*=n;
n--;
}
printf("%ld\n",fact);
}
int main()
{
long int n;
while(scanf("%ld",&n)==1)
{
if(n<0 && n%2==0)
printf("Underflow!\n");
else if(n<0 && n%2!=0)
printf("Overflow!\n");
else if(n==0||n<=7)
printf("Underflow!\n");
else if(n>=14)
printf("Overflow!\n");
else
factorial(n);
}
return 0;
}
Next Previous