-
factorial problem
write a program that evaluates the factorial of the integer 1 to 5. print result in tabular format.
Code:
#include <stdio.h>
long factorial(long number);
int main()
{
int i,number;
for (i=1;i<=5; i++) {
printf("%2d!=%1d\n", i, factorial (number));
}
return 0;
}
long factorial(long number)
{
if (number <=2) {
return 1;
else {
return (number*factorial(number-1)); }
}
}
-
Try
printf("%2d!=%li\n", i, factorial (i));
-
Solved
Well the logic given by our friend is right
use factorial(i)
i took the pain of compiling it in TurboC
here is the debugged program
Code:
#include <stdio.h>
#include<conio.h>
long factorial(long number);
int main()
{
int i,number;
for (i=1;i<=5; i++) {
printf("%2d!=%1d\n", i, factorial (i));
}
getch();
return 0;
}
long factorial(long number)
{
if (number <=2)
{
return 1;
}
else
{
return (number*factorial(number-1));
}
}
:D
-
thanks for the reply but all the number need to mutiply by 2 to be correct.
example from your program
6! = 60 but it suppose ot equal 120
-
> if (number <=2)
Probably because this 2 should be 1
-
Thanks everyone for the help, I finally got the program to run