Hello ..i am trying to get the factorial of large integers but i get 0 every time i try,,can anyone tell me why ,,,Thanks in advance .

``````#include<stdio.h>
#include<conio.h>
int factorial(int n);
//main
main()
{
int x,fact;
printf("Enter a positive interger \n");
scanf("%d",&x);
fact=factorial(x);
if (fact==-1)
printf("the number is not positive !!");
else
printf("The factorial of %d is %d\n",x,fact);
getch();
}

//A function to compute the factorial
int  factorial(int n)
{
if (n<0)
return -1;
if (n<2)
return 1;
else
return (n*factorial(n-1));
}``````
``````#include<stdio.h>
#include<conio.h>
int factorial(int n);
//main
main()
{
int x,fact;
printf("Enter a positive interger \n");
scanf("%d",&x);
fact=factorial(x);
if (fact==-1)
printf("the number is not positive !!");
else
printf("The factorial of %d is %d\n",x,fact);
getch();
}

//A function to compute the factorial
int  factorial(int n)
{
if (n<0)
return -1;
if (n<2)
return 1;
else
return (n*factorial(n-1));
}
``````

I am sorry I did not understand what is your problem ?I ran the code that you have posted for 10! and I got the answer. So what is the problem ?
These are the problems that I see with your code
You have not checked for overflow. If you enter as i/p a very large number then the factorial of that number will not be able to fit in the 32 bit (or what ever is the size of int in your system) and so the answer will be negative
You have used the non portable header file conio.h. Also use int main as opposed to just main

Maybe because of the limits of the integer type. Integers can hold numbers up to 32768 ( as i remember ). So any value greater than 2^16 will overflow and give you uncorrect results. You may try using double or a user created type to hold values for factorials as factorials can overgrow quickly.

Thanks To All ,,You are right ,,i got it !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.