Help with C programming

lankygiant16

New Member
Im trying to do a so called "simple loop" using C,,,but i cant get it to work...this is what i have

#include <stdio.h>

int main (void)
{
int fac=1, i, num;

printf( "Enter a number: ");
scanf( "%i\n", &num);

for(i=1; i<=num; i++);
{

fac = fac * i;

printf("\nFactorial of Num is %i\n", fac);
}
return 0;
}




and what happens is i get the Enter number...i enter the number....then the program does nothing, so i enter the number again, and it gives an answer of the number entered + 1. So like if i entered in 10, it'd say the factorial is 11....which is wrong...and i dont know why its just adding one AND why its not even doing the loop. PLEASE HELP!!!!!!!!
 
The first thing you have to do is remove the \n from your scanf. Let me find my compiler and I'll work on the rest. I think it's a logic error.
 
ok thank you....god i hate this class....i love computers...but my teacher doesnt do much...he teaches us the basics of each chapter, then has us go out on our own and try to do crap like this....w/o explaining much of it. No one in my class of 20 people got this done, and it was supposed to be due at the end of class....its crap lol.
 
You'll have more luck if you remove the ; after the for statement :) (in addition to what Cromewell said)

Also put the final printf outside the loop.
 
ah damnit...I see the problem now. It's a silent syntax error.
Code:
for(i=1; i<=num; i++); // <--this semicolon shouldn't be here
{

ah ceewi1 beat me to it, I completely missed that until I retyped the seemingly not working for loop.
 
well crap....lol i hate trying to do this stuff w/o being taught how. I got the other step i needed to do, but the last step has me confused. It says

"Divide the number (entered) by two (drop remainder). Output out the new value and contine dividing and outputting the update value as long as he updated value is greater than 0. EXAMPLE: If 8 were entered, your program should output (only): 4, 2, 1"

and heres what i got for that part "including the last for statement + another step"


int fac=1, i, num, sum=0, div;

printf( "Enter a number: ");
scanf( "%i", &num);

for(i=1; i<=num; i++)
{

fac = fac * i;
sum = sum + i;


while(div > 0)

div = num / 2;

printf("\n %i\n", div);
}

printf( "\nThe Factorial of the number is %i\n", fac);
printf( "\nThe summation of the number is %i\n", sum);



i tried "zero-ing" out div...no go....tired other combos...still none...am i using the right loop? The words "as long as" pointed me towards the while statement. Thanks
 
UPDATE....sooooo i got the program to divide by two...i kinda just stumbled onto it. But idk how to not have it print out 0......he only wants 4, 2, 1 (if 8 was entered) this is what i got now


int fac=1, i, num, sum=0;

printf( "Enter a number: ");
scanf( "%i", &num);

for(i=1; i<=num; i++)
{

fac = fac * i;
sum = sum + i;
}

while(num > 0)
{
num = num /2;

printf("\n %i\n", num);
}

printf( "\nThe Factorial of the number is %i\n", fac);
printf( "\nThe summation of the number is %i\n", sum);
 
The problem is with this section:
Code:
while(num > 0)
{
num = num /2;

printf("\n %i\n", num);
If num = 1, the loop is entered. num is then halved to 0, and printed.

To fix it, you could either implement a check last loop, or simply change the (num > 0) to (num > 1).
 
Back
Top