Problem Type : Number Theory
#include<stdio.h>
#define max 15
#define len 200
int fact[max][len];
void factorial()
{
fact[0][0]=1;
fact[1][0]=1;
int i,j,k;
for(i=2; i<max; i++)
{
for(j=0; j<len; j++)
{
fact[i][j] = fact[i][j] + fact[i-1][j]*i;
if(fact[i][j]>9)
{
fact[i][j+1] = fact[i][j]/10;
fact[i][j] = fact[i][j]%10;
}
}
}
}
int main()
{
factorial();
int i,j,n;
while(scanf("%d",&n)==1)
{
if(n>=0)
{
if(n<8)
{
printf("Underflow!\n");
}
else if(n>13)
{
printf("Overflow!\n");
}
else
{
for(i=len-1; i>=0; i--)
{
if(fact[n][i]!=0)
break;
}
for(j=i; j>=0; j--)
{
printf("%d",fact[n][j]);
}
printf("\n");
}
}
else if(n<0)
{
if(n%2==0)
{
printf("Underflow!\n");
}
else
{
printf("Overflow!\n");
}
}
}
return 0;
}
#include<stdio.h>
#define max 15
#define len 200
int fact[max][len];
void factorial()
{
fact[0][0]=1;
fact[1][0]=1;
int i,j,k;
for(i=2; i<max; i++)
{
for(j=0; j<len; j++)
{
fact[i][j] = fact[i][j] + fact[i-1][j]*i;
if(fact[i][j]>9)
{
fact[i][j+1] = fact[i][j]/10;
fact[i][j] = fact[i][j]%10;
}
}
}
}
int main()
{
factorial();
int i,j,n;
while(scanf("%d",&n)==1)
{
if(n>=0)
{
if(n<8)
{
printf("Underflow!\n");
}
else if(n>13)
{
printf("Overflow!\n");
}
else
{
for(i=len-1; i>=0; i--)
{
if(fact[n][i]!=0)
break;
}
for(j=i; j>=0; j--)
{
printf("%d",fact[n][j]);
}
printf("\n");
}
}
else if(n<0)
{
if(n%2==0)
{
printf("Underflow!\n");
}
else
{
printf("Overflow!\n");
}
}
}
return 0;
}
No comments:
Post a Comment