# Thread: having a problem with a FOR statement

1. ## having a problem with a FOR statement

This is a project I have for school. I am asking for help with my homework. I keep getting 39 as the answer to this program. The program is supposed to be Factorial, meaning that when you enter in an integer it will multiply itself and then by its decremented self for example 3!=3*2*1 ==6. Well i think I have the FOR statement right but it keeps giving me the wrong answers. Here is the code I have got.

#include <stdio.h>

int fact(int d);
void print_out (int d);
void main (void)
{
int b,y;
printf("enter a positive integer:\n");
scanf("%d",&b);
y=fact(b);
print_out(y);
}

int fact(int d) //
{
int c,a;

for(d=c;d>1;--d)
{
a=c*d;
}
return(a);
}

void print_out (int d)
{
printf("After the Factorial the number is:%d",d);
}

this is my problem 2. Look at your fact function more closely. You pass it whatever you enter has the parameter, and then change it in your loop... 3. Watch the initialisation

int c,a = 1;
for(c=d;c>1;--c)
{
a=a*c;
}

It might work... 4. As govtcheez says it's your for loop....I am thinking maybe you want for( c = d; c > 1; c--)....and post decrement Popular pages Recent additions 