• Welcome to Smashboards, the world's largest Super Smash Brothers community! Over 250,000 Smash Bros. fans from around the world have come to discuss these great games in over 19 million posts!

    You are currently viewing our boards as a visitor. Click here to sign up right now and start on your path in the Smash community!

Computer Programmers, I need your help!

Sizzle

I paint controllers
Joined
Aug 1, 2005
Messages
1,466
Location
Hirosaki, Japan / San Diego State
I'm a first year computer science major, and we are learning the Java programming language. My goal, write a program that gives the factorial of a user inputted number. Ex. 6! = 6*5*4*3*2*1, where 6! is read six factorial. I know I need to use a FOR loop to accomplish this but I think I also need to use IF and ELSE statements as well. If anyone could gimme a hint or two, that would be greatly appreciated.
 

Haruka's DNA

Smash Cadet
Joined
Mar 11, 2007
Messages
43
Location
Phoenix, AZ
AltF4:

Here ya go, might have some syntax errors or something, but this is pretty much it:

Iteratively:

public Factorial(int n)
{
int total = n;
n--;

while(n > 1)
{

total *= n;
n--;

}
return total;

}



Recursively:

public Factorial(int n)
{

if(n == 0)
{
return 1;
}
else
{
return n * Factorial(n-1);
}
}
 
Top Bottom