×

# FCTRL: Getting compile error

 0 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 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 0★heena123 31●2●2●5 accept rate: 0% 0★admin ♦♦ 19.8k●350●498●541

 0 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? answered 04 Jul '12, 14:57 16.9k●49●115●225 accept rate: 11% 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)
 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:

×525
×196
×33
×19

question asked: 04 Jul '12, 00:54

question was seen: 1,000 times

last updated: 09 Jul '12, 10:21