×

# Small Factorial

 0 //my code for smallfactorial,it's giving correct output in my system but not here..please tell me whats wrong in it??? #include #include int fact(int); int main() { int t,n,r; scanf("%d",&t); while(t--) { r=0; scanf("%d",&n); r=fact(n); printf("%d\n",r); } return 0; } int fact(int n) { if(n==1) return 1; else return(n*fact(n-1)); }  asked 06 Feb '14, 21:14 14●1●2●2 accept rate: 0% 4★kunal361 6.0k●13●32●72

 0 it will not work for larger inputs...by larger i mean nos like 20-30 will also give a WA...as integer will overflow!!! try putting 100...the worst case!!! answered 06 Feb '14, 21:23 4★kunal361 6.0k●13●32●72 accept rate: 21%
 0 @kunal361- thanks for your ans,u r ri8 but i used unsigned long integer variable for storing the result but it is still not working... answered 06 Feb '14, 21:30 14●1●2●2 accept rate: 0%
 0 shivali24: Just think about it for a while. What's the range of int? Approximately 10^9. What's the range of long long? Approximately 10^18. How large can the result get? Try a calculator: 30! is around 10^32. Well... shit. Of course it won't work with any integer type of C/C++. Try using "bigint"s - represent a number as an array of base-10 digits. (It works a bit faster if you use a larger base.) You just need to implement the addition and multiplication of such numbers, which is easy enough in O(number of digits ^2) in the same way as you'd do it on paper. answered 06 Feb '14, 22:07 7★xellos0 5.9k●5●43●93 accept rate: 10%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×196
×33

question asked: 06 Feb '14, 21:14

question was seen: 961 times

last updated: 06 Feb '14, 22:10