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

×

what's wrong with the following small factorial solution?

include<stdio.h>

int main() { int i,j,t,x,fact=1; scanf("%d",&t); for(i=0;i<t;i++) { scanf("%d",&x); for(j=1;j<=x;j++) { fact=fact*j; } printf("%d\n",fact); } return 0; }

asked 03 Apr '14, 16:22

sameerkhan005's gravatar image

1★sameerkhan005
12
accept rate: 0%


@sameerkhan005 : You can't store factorials of numbers upto 100 in "integer" or "long" or "long long" . You need to implement some sort of "BigInteger" class for this problem .

link

answered 03 Apr '14, 16:32

vineetpaliwal's gravatar image

6★vineetpaliwal
12.4k47107171
accept rate: 12%

@sameerkhan005: you have to initialize fact to 1 in the outer 'for' loop. In your code when you take second input for calculating the factorial, previous result is already in the 'fact' variable. To correct this you just have to insert the statement "fact=1;" just before or after "scanf("%d",&x);".

link

answered 03 Apr '14, 17:35

satyam94_'s gravatar image

4★satyam94_
11
accept rate: 0%

1

yes that is another bug . But you wont get "Correct Answer" till you handle overflow

(03 Apr '14, 17:53) vineetpaliwal6★
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:

×46
×33

question asked: 03 Apr '14, 16:22

question was seen: 707 times

last updated: 03 Apr '14, 17:53