Author |
Message |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Fri Oct 08, 2010 7:57 pm Post subject: X to the power N |
|
|
Hi, I have been trying to solve the following question:
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
Java: |
import java.util.*;
public class Q6
{
public static void main (String[] args )
{
Scanner in = new Scanner (System. in);
float x;
int n;
double x_n = 1. 0;
System. out. println ("Enter X");
x = in. nextFloat();
System. out. println ("Enter N");
n = in. nextInt();
if (n > 0){
for (int c = 1; c <= n; c++ );
{
x_n = x_n * x;
}
System. out. print ("\n" + x + " to the " + n + " power is: " + x_n );
}
//rest of program follows
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Fri Oct 08, 2010 8:09 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
DanShadow
![](http://compsci.ca/v3/uploads/user_avatars/12142970654c996e83e6997.png)
|
Posted: Fri Oct 08, 2010 9:16 pm Post subject: 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.
Quote: x = in.nextFloat(); |
|
|
|
|
![](images/spacer.gif) |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Sat Oct 09, 2010 9:24 am Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
A.J
![](http://compsci.ca/v3/uploads/user_avatars/119833057151651227b0d87.gif)
|
Posted: Sat Oct 09, 2010 9:55 am Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Sat Oct 09, 2010 10:09 am Post subject: RE:X to the power N |
|
|
I created else ifs for n = 0 and n < 0. |
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Sat Oct 09, 2010 10:43 am Post subject: RE:X to the power N |
|
|
Remove the semi-colon after your for loop. |
|
|
|
|
![](images/spacer.gif) |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Sat Oct 09, 2010 1:46 pm Post subject: Re: RE:X to the power N |
|
|
chrisbrown @ Sat Oct 09, 2010 10:43 am wrote: Remove the semi-colon after your for loop.
Thanks so much!!! That fixed the problem!!! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Sat Oct 09, 2010 10:36 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Sun Oct 10, 2010 2:14 pm Post subject: Re: RE:X to the power N |
|
|
copthesaint @ Sat Oct 09, 2010 10:36 pm wrote: *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.
Thats the problem with me doing java....
I put semicolons where they aren't needed and don't put them where they are. |
|
|
|
|
![](images/spacer.gif) |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Mon Oct 11, 2010 1:57 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
INTELKING
|
Posted: Mon Oct 18, 2010 3:19 pm Post subject: Re: X to the power N |
|
|
Interesting...Thanks. |
|
|
|
|
![](images/spacer.gif) |
|