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

×

FCTRL: Getting compile error

I am getting compile error -In function 'int main()': prog.cpp:17: error: a function-definition is not allowed here before '{' token prog.cpp:26: error: expected `}' at end of input Please have a look at my code : #include<stdio.h> int fac(int n);

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

int fac(int n)
{
while(n)
{
fact*=n;
n--;
}
return fact;
}
return 0;
}

asked 04 Jul '12, 00:54

heena123's gravatar image

0★heena123
31225
accept rate: 0%

edited 09 Jul '12, 10:21

admin's gravatar image

0★admin ♦♦
19.8k350498541


Are you sure you want to have fac function in main function?

edit: Yes, this is the problem...

And fact variable? I quite sure that you do NOT want to share this variable too...

Let's assume your code works well (=no compilation error). When you use input from problem statement

4
1
2
5
3

You will get

1 (ok)
2 (ok)
240 (not ok)
720 (not ok)

Look also on Factorial problem - from example input/output you can see, that 100! ends with 24 zeros, do you know what is the biggest int value?

link

answered 04 Jul '12, 14:57

betlista's gravatar image

3★betlista ♦♦
16.9k49115225
accept rate: 11%

edited 04 Jul '12, 16:38

Is that going to create a problem ?if I declare outside main() then where should I declare fact it is used in both fac() and main()

(04 Jul '12, 16:18) heena1230★

@heena123: Please, see the edited answer (it's too big for comment) ;-)

(04 Jul '12, 16:39) betlista ♦♦3★
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:

×525
×196
×33
×19

question asked: 04 Jul '12, 00:54

question was seen: 1,000 times

last updated: 09 Jul '12, 10:21