yesterday i was just checking out the factorial(recursion) program once again to see something.The output for the program upto 12 was correct but for number >12 it gave wrong output. Confused with it i used the long integer for it also.why is it so??Can you find out the problem
Code:
#include<stdio.h>
long int factorial(int);//function prototype
int main()
{
int num;
long otpt;
FILE *fp=fopen("Output.txt","a");//file to keep track of output
printf("\nPLEASE ENTER THE VALUE WHOSE FACTORIAL IS REQUIRED:\n");
scanf("%d",&num);
while(num>20||num<1)//range should be between 1 & 20
{
printf("\nERROR-PLEASE ENTER BETWEEN 1 & 20: ");
scanf("%d",&num);
}
otpt=factorial(num);
fprintf(fp,"INPUT: %d",num);
fprintf(fp,"\nOUTPUT: %ld\n\n",otpt);
printf("\nTHE FACTORIAL IS: %ld",otpt);
fclose(fp);
return 0;
}
long int factorial(int x)
{
if(x<=1)
return 1;
else
return(x*factorial(x-1));
}