FCTRL: Getting compile error

compile
error
exception
factorial

#1

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);
fact=fac(n);
printf(”%d
",fact);
}

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

#2

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?


#3

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()


#4

@heena123: Please, see the edited answer (it’s too big for comment) :wink: