Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 09-26-2006, 12:26 AM   #1
Baseband Member
 
lankygiant16's Avatar
 
Join Date: Aug 2006
Posts: 63
Default 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
__________________

lankygiant16 is offline   Reply With Quote
Old 09-26-2006, 06:34 AM   #2
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default 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;
}

n is the number that you start with
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."
root is offline   Reply With Quote
Old 09-26-2006, 04:53 PM   #3
Baseband Member
 
lankygiant16's Avatar
 
Join Date: Aug 2006
Posts: 63
Default Re: Doing factorials using C

ahhh ok so i was close....just 1 little mistake. Thanks a bunch man.
lankygiant16 is offline   Reply With Quote
Old 09-27-2006, 12:33 AM   #4
Baseband Member
 
lankygiant16's Avatar
 
Join Date: Aug 2006
Posts: 63
Default Re: Doing factorials using C

arg it still isnt working for me.....this is what i have typed in:


#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!!!!!!!!
lankygiant16 is offline   Reply With Quote
Old 09-27-2006, 04:39 AM   #5
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default 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 <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;
}
__________________
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."
root is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 06:38 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0