The following code finds out the factorial of a number. There is some error in it. Can you fix the bug?
#include
int main ()
{
long number;
std::cin>>number;
std::cout<< factorial (number);
return 0;
}
long factorial (long a);
{
for(int i=1;i<=a;i++)
fact=fact*i;
}
Answers
Answered by
2
Answer:
before main function you have to declare your user defined function.
Explanation:
i mean write
long factorial(long a);
before main function
Answered by
14
Answer:
#include <iostream>
long factorial (long a);
int main ()
{
long number;
std::cin>>number;
std::cout<< factorial (number);
return 0;
}
long factorial (long a)
{
int fact=1;
for(int i=1;i<=a;i++)
fact=fact*i;
return fact;
}
Explanation:
function declaration isn't there.
fact isn't returned. i.e there will be no output.
fact value hasn't been initialized , hence the loop becomes invalid
Similar questions