Computer Forums Doing factorials using C

 09-25-2006, 11:26 PM #1 Baseband Member     Join Date: Aug 2006 Posts: 63 Doing factorials using C So im taking this C programming class, and im not too great at programming, so im having a tough time. My teacher sucks, he doesnt really explain things very well, so we have to figure a lot stuff out. Im having trouble doing factorials...and yeah. this is how i first took a stab at it int i, num, factorial; for( i=1; i<=num; i++) { factorial = num * i; } now i know this is wrong...just HOW WRONG am i? and how do i fix it. I also have to do summation meaning like 5+4+3+2+1 as an example....so i figure it will be quite similar. thanks for all the help!! Jim __________________ __________________
09-26-2006, 05:34 AM   #2
Site Team

Join Date: Mar 2004
Posts: 8,098
Re: Doing factorials using C

OK...

first think about what a factorial is...

Quote:
 1! = 1 = 1 2! = 2 x 1 = 2 3! = 3 x 2 x 1 = 6
so to find a factorial number you need to multiply all the numbers below it in sequence until you get to the at number...

for instance 4! = 1 * 2 * 3 * 4 = 24

so you just need a simple loop,
n = [start number];
factorial = 1;
for (i=1;i<=n;i++)
{
factorial = factorial * i;
}

factorial is the total...

put the number 5 into the loop and you get something like...
n=5
(i=1) (factorial=1) -> (new factorial = 1*1 = 1)
(i=2) (factorial=1) -> (new factorial = 1*2 = 2)
(i=3) (factorial=2) -> (new factorial = 2*3 = 6)
(i=4) (factorial=6) -> (new factorial = 6*4 = 24)
(i=5) (factorial=24) -> (new factorial = 24*5 = 120)

now i=t so the loop stops and you have the numbers...
n=5
factorial = 120.
__________________

__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."

 09-26-2006, 03:53 PM #3 Baseband Member     Join Date: Aug 2006 Posts: 63 Re: Doing factorials using C ahhh ok so i was close....just 1 little mistake. Thanks a bunch man. __________________
 09-26-2006, 11:33 PM #4 Baseband Member     Join Date: Aug 2006 Posts: 63 Re: Doing factorials using C arg it still isnt working for me.....this is what i have typed in: #include 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!!!!!!!! __________________
 09-27-2006, 03:39 AM #5 Site Team     Join Date: Mar 2004 Posts: 8,098 Re: Doing factorials using C the problem is that you have a semi colon on the start of the loop line. for(i=1; i<=num; i++); remove that and it should all work fine. #include 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; } __________________ I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."