New programmer - please help - what have I done wrong

J

Johnny

I'm just starting to learn Java and I'm trying to write some code for
computing a factorial. (That the mathmatical expression for a number
to it's own power). I'm writing this with Sun One studio 4. Here is
what I have, the problem is that it's not working. When I enter 3 I
get back 81 (should get 27), when I enter 4 I get 65536 (should get
256):

public class Factorial
{


public static void main(String[] args)
{
int x,y,q;
Keyboard kbd;
kbd = new Keyboard();
y=1;

System.out.println("Please enter a number you'd like the factorial
for");
q=kbd.readInt();
x=q;
if (q==0)
{
System.out.println("The factorial is 1");
}
else
{
while (y<q)
{
y=y+1;
x=x*x;
}System.out.println("The factorial is "+x);
}

}

}

Any suggestions?

If possible please email me at (e-mail address removed)
 
J

Joona I Palaste

Johnny said:
I'm just starting to learn Java and I'm trying to write some code for
computing a factorial. (That the mathmatical expression for a number
to it's own power). I'm writing this with Sun One studio 4. Here is
what I have, the problem is that it's not working. When I enter 3 I
get back 81 (should get 27), when I enter 4 I get 65536 (should get
256):

First off, that's not a factorial. A factorial is every number from 1
to your number multiplied together. There is no fancy name for a number
to its own power. But assuming you still want to calculate a number to
its own power:
public class Factorial
{


public static void main(String[] args)
{
int x,y,q;
Keyboard kbd;
kbd = new Keyboard();
y=1;

System.out.println("Please enter a number you'd like the factorial
for");
q=kbd.readInt();
x=q;
if (q==0)
{
System.out.println("The factorial is 1");
}
else
{
while (y<q)
{
y=y+1;
x=x*x;
}System.out.println("The factorial is "+x);

Your algorithm is faulty. It keeps squaring x on every iteration, when
it should be multiplying it with the original number. Try making the
"x=x*x" bit "x=x*q" and see if it works.
Any suggestions?
If possible please email me at (e-mail address removed)

Ask here, read here.
 
C

chris sennitt

well - a factorial is not the number raised to its own power
4 factorial is 4*3*2*1
3 facorial is 3*2*1 == 6

glancing at your code try changing the line
x = x * x
to
x = x * y
 
J

Joona I Palaste

chris sennitt said:
well - a factorial is not the number raised to its own power
4 factorial is 4*3*2*1
3 facorial is 3*2*1 == 6
glancing at your code try changing the line
x = x * x
to
x = x * y

....if he wants to calculate a factorial.
If he wants to calculate what he said he wants, it should be
x = x * q.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top