You are not logged in. Please login at www.codechef.com to post your questions!

×

What's wrong with this code?

include<stdio.h>

int main() { int t,i,n; int f; scanf("%d",&t); while(t>0) { f=1; scanf("%d",&n); for(i=1;i<=n;i++) f=f*i; printf("%d\n",f); t--; } return 0; }

asked 05 Jun '18, 20:49

mercy779's gravatar image

0★mercy779
1
accept rate: 0%

What error are you getting by this code??

(05 Jun '18, 21:16) ssp5473★

The problem with your code is that you are trying to store a number which is out of the range of int. It wouldn't have mattered even if you would have used long long because range of long long is roughly 10^19(approx 20 digits) and 100! itself has 21 zeroes at the end. So in long long you can only store upto 20! or 21!.This is why your code is wrong, assuming you have to calculate factorial of numbers greater than 20 or 21.

link

answered 05 Jun '18, 21:36

swapnil159's gravatar image

5★swapnil159
2365
accept rate: 17%

edited 05 Jun '18, 21:36

Your code is right but you see factorials of a number is very large int can't store above factorial of 20( that too i doubt) even long long or unsigned 64-bit integer can't store value of 100!(unsigned 64-bit integer can store value upto 19 digits whereas 100! is 150+ digits)you need to be cleverer here see this has the solution http://iamit.in/blog/Spoj-Problem-Small-Factorials-FCTRL2-Explanation-and-Solution/

link

answered 07 Jun '18, 15:13

ssp547's gravatar image

3★ssp547
3077
accept rate: 25%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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:

×255
×196
×33

question asked: 05 Jun '18, 20:49

question was seen: 132 times

last updated: 07 Jun '18, 15:13