Write a program that computes x^y

cansomeone help me in doing this program: Write a program that computes x^y, where x and y are both real nos.thanks.. im new in java programming, can someone give me the code so that i can study it. thanks please.
[234 byte] By [grace_a] at [2007-10-3 4:11:12]
# 1
java.lang.Math
sabre150a at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 2
Math.pow(x, y)
r035198xa at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 3

Now just put those two together.

first of all, you need to put the line

import java.lang.Math

at the beginning of your program. Then, you need to initialise values to x and y, maybe have a user input there. Then, use what r035198x said, Math.pow(x,y);

.

Im assuming you know how to do the rest.

merlingorea at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 4
> first of all, you need to put the line > import java.lang.MathYou don't need to import java.lang.Math or any part of lava.lang package. They are implicitly imported.
sabre150a at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 5
> first of all, you need to put the line > import java.lang.Math> at the beginning of your program. No he does not.
r035198xa at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 6
Oh, i didn't know that. Its always automatically imported? alright, my mistake, sorry.
merlingorea at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 7
Check out this link http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.htmland this thread: http://forum.java.sun.com/thread.jspa?forumID=54&threadID=732629JJ
Java_Jaya at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 8

> thanks.. im new in java programming, can someone give

> me the code so that i can study it. thanks please.

Whilst all the answers above are what you would do in practice, I suspect that what your teacher wants is for you to do this from first principles (using the features of the language such as loops, arithmetic, if-else constructs etc) and not by using some standard library function.

So you are designing an algorithm to raise x to the power of y. Try and do it in pseudo-code first (pseudo-code is not really code; it's like a human-readable set of instructions such as "add x to y and place result in variable z" or whatnot). SO think about the algorithm first and then worry about converting it into Java code later.

If you post your pseudo-code algorithm, I'm sure people here (including me) will help you Java-ify it.

oxbow_lakesa at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 9

thanks for your replies guys. =)

anyway, as oxbow_lakes said, i already have this idea on me.

the pseudo-code is something like this:

let x be the base.

let y be the exponent.

if y is 0, x is the answer.

if not,

for(n =0 to y-1)

x*=x

n++

then x is the answer..

how exactly would i implement that in java?

is that im going to use the pow function only?

thanks guys for helping me.

grace_a at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 10
>if y is 0, x is the answer.Are you sure?>how exactly would i implement that in java? Well, you almost have it, just add some braces and make little changes to syntax - I guess that half an hour of reading will help you do it right.Mike
bellyrippera at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 11
> for(n =0 to y-1)> x*=x> n++> > then x is the answer..Again are you sure?You need to review the mathematical definition of the operator.
ejpa at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 12
oooops. sorry.. what i mean is, if y is 0, then the answer is 1. =)if y is one, then x is the answer..
grace_a at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 13

So start with a basic Java Hello World example. Have you got that to work? You can modify a simple Hello World program to do what you want. By the way, a Java for loop looks like this:

for (int i = 0; i < 10; i++) {

System.out.println("i = " + i);

}

So it's for( initialize ; test-condition ; increment )

oxbow_lakesa at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 14
how about using the math.pow(x,y)?ive learned that this math function is very sensitive in that the values of x, y should be considered first before using it.
grace_a at 2007-7-14 22:11:39 > top of Java-index,Java Essentials,Java Programming...
# 15
> how about using the math.pow(x,y)?> > ive learned that this math function is very sensitive> in that the values of x, y should be considered first> before using it.In what way 'very sensitive'?
sabre150a at 2007-7-21 10:25:10 > top of Java-index,Java Essentials,Java Programming...
# 16
It's very easy to make Math.pow cry...even a sad movie will do it.Yes, it's that sensitive.
-Kayaman-a at 2007-7-21 10:25:10 > top of Java-index,Java Essentials,Java Programming...
# 17
The scope of her homework probably specifically excludes using Math.pow(x,y), since I imagine this is a assignment that deals with operators and looping.J
jagulara at 2007-7-21 10:25:10 > top of Java-index,Java Essentials,Java Programming...