Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This code finds the factorial for a given number but this code is only able to find the factorial for only smaller numbers, when larger numbers like 100 or more it shows zero and the output is like "The factorial of 100 is 0", could you please fix this code for me.

What I have tried:

#include<stdio.h>
unsigned long long factorial(unsigned long long x);
int main(void)
{
     unsigned long long a=100, fact;
     
     fact= factorial(a);
     
     printf("the factorial of %llu is %llu",a,fact);
}

unsigned long long factorial(unsigned long long x)
{
            unsigned long long f;
	if(x==1)
	return (1);
	
	else
	
	f= x*factorial(x-1);
	return (f);
}
Posted
Updated 30-Jan-18 5:51am
v2
Comments
F-ES Sitecore 30-Jan-18 11:51am    
Probably because unsigned long isn't big enough to hold 100!

1 solution

The max value of an unsigned long long[^] is 264-1. 100! factorial is way bigger than that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900