
-----------------------------------
ProgrammingFun
Fri Oct 08, 2010 7:57 pm

X to the power N
-----------------------------------
Hi, I have been trying to solve the following question:


Write a program that computes  where  is a floating point number and  is a positive integer. The program informs the user that  must be positive if the user enters a negative value. Of course, 
XN = X * X * X * ... * X   
     --------------------
           N times


I wrote the following code but the program doesn't multiply properly.
Any ideas on what I may be doing wrong?
Thanks


   import java.util.*;

   public class Q6
   {
      public static void main (String

-----------------------------------
DtY
Fri Oct 08, 2010 8:09 pm

RE:X to the power N
-----------------------------------
Is that what the question is, verbatim? It doesn't make any sense.

I gather the problem is to write a program that will calculate x to the power of n, where x is a floating point number, and x is a natural number?

Are you just getting unexpected output? The program looks fine to me, I'd suggest comparing what the program is giving you with what it should be doing, if it's a power of x times too big or small, the problem is probably the number of times the loop is running.

-----------------------------------
DanShadow
Fri Oct 08, 2010 9:16 pm

RE:X to the power N
-----------------------------------
Your program logic looks fine.
My suggestion: use the Java function Sun made for this ;)

Math.pow(x,n);

Personally i'd swich your use of the float datatype to double though.
x = in.nextFloat(); 

-----------------------------------
ProgrammingFun
Sat Oct 09, 2010 9:24 am

RE:X to the power N
-----------------------------------
The problem with using the Math.pow function is that we are supposed to use only loops for the question.  I still can't get the program to give proper output.

For example: 

Enter X: 5
Enter Y: 5

5 to the power 5 is 5.0.

-----------------------------------
A.J
Sat Oct 09, 2010 9:55 am

RE:X to the power N
-----------------------------------
Well, for starters, make sure you move your output statement to outside of the 'if(n>0)' statement, as if n = 0 your program won't output anything.

-----------------------------------
ProgrammingFun
Sat Oct 09, 2010 10:09 am

RE:X to the power N
-----------------------------------
I created else ifs for n = 0 and n < 0.

-----------------------------------
chrisbrown
Sat Oct 09, 2010 10:43 am

RE:X to the power N
-----------------------------------
Remove the semi-colon after your for loop.

-----------------------------------
ProgrammingFun
Sat Oct 09, 2010 1:46 pm

Re: RE:X to the power N
-----------------------------------
Remove the semi-colon after your for loop.
Thanks so much!!! That fixed the problem!!!

-----------------------------------
copthesaint
Sat Oct 09, 2010 10:36 pm

RE:X to the power N
-----------------------------------
*facepalm* I laughed so hard when I saw the semi colon after the for loop, I was going to just say something now, but I see chrisbrown beat me too it.

-----------------------------------
ProgrammingFun
Sun Oct 10, 2010 2:14 pm

Re: RE:X to the power N
-----------------------------------
*facepalm* I laughed so hard when I saw the semi colon after the for loop, I was going to just say something now, but I see chrisbrown beat me too it.
 :oops: 
Thats the problem with me doing java....
I put semicolons where they aren't needed and don't put them where they are.

-----------------------------------
DtY
Mon Oct 11, 2010 1:57 pm

RE:X to the power N
-----------------------------------
Haha, didn't see that semicolon.

Just remember, you have to have a complete statement after an if or a loop.

A complete statement is either a block of { to }, or something ending in a semicolon. When a semicolon directly follows an if or a loop, that is the statement attached to it (it's nothing). 

The fun part comes where Java lets you put blocks of { to } without being attached to an if or loop.

-----------------------------------
INTELKING
Mon Oct 18, 2010 3:19 pm

Re: X to the power N
-----------------------------------
Interesting...Thanks.
