# Program to compute powers

• October 25th, 2010, 11:43 AM
Shyamz1
Program to compute powers
Hi there folks.

I'm trying to create a program that takes 2 numbers from command line arguments and use the second value as a power so for e.g. java Power 10 2 should give 100. But i want to do this without using the Math.pow method, and using a for loop. I have the following so far but i'm not sure what the problem is and it;s only seem to be doinng square numbers (i.e. to the power of 2)
Code java:

```public class Power { public static void main(String [] args) { int mantissa = Integer.parseInt(args[0]); int exponent = Integer.parseInt(args[1]); int answer;     for(int i=0; (mantissa*mantissa) < exponent; i++) exponent=exponent + i;   answer = mantissa*mantissa; System.out.println("Result is " + answer);     } }```
• October 25th, 2010, 12:50 PM
Darryl.Burke
Re: Program to compute powers
Well, all you ever set the value of answer to is mantissa*mantissa so what did you expect?

Put your code aside. Take a paper and write down the steps to get a power in simple English (or your native language). Check your steps with 3 or 4 sample data sets. When it's all correct (and not before!), convert those steps into Java code.

db
• October 25th, 2010, 01:15 PM
Shyamz1
Re: Program to compute powers
I know how to get powers, i wrote pseudocode and designed it, but im not sure what the case is for like cubics and powers more than 2.
• October 25th, 2010, 04:32 PM
javapenguin
Re: Program to compute powers
You know there already is a class called Math that has a method called power that takes two numbers and raises the first number to the power of the second number.
• October 25th, 2010, 04:48 PM
javapenguin
Re: Program to compute powers
Code java:

```public power(int num, int power) { int num2; if (power == 0 && num!=0) { num2 = 1; } else if (power == 1) { num2 = num; }   else if (1 < power) { num2 = num * power(num, power -1); }   else if(0 > power && num!=0) { num2 = 1/power(num, - power); } }```

Well...that may not quite work, haven't tried it out yet, but it's on the right track.
• October 25th, 2010, 04:51 PM
helloworld922
Re: Program to compute powers
You're algorithm doesn't compute x^y.

Get out a piece of paper and "execute" the code as if you were the computer (pick some sample inputs):

ex. mantissa = 4, exponent = 3

4^3 = 64

Code Java:

``` for(int i=0; (mantissa*mantissa) < exponent; i++) exponent=exponent + i;```
1. initialize i = 0
2. 4 * 4 = 16
3. exponent = 3
4. 16 < 3?
5. false, exit the for loop

Code Java:

```answer = mantissa*mantissa; System.out.println("Result is " + answer);```
6. answer = 4 * 4 = 16
7. Print out "result is 16"

Re-visit your pseudo-code and make sure that that is correct.

As far as I can tell, your code at best will give you mantissa^2, and at worst will keep you stuck indefinately inside the for-loop (well, not really indefinately, integers wrap around at 2^31 so eventually your code will stop and give you mantissa^2)

edit:

Quote:

Code Java:

```public power(int num, int power) {   if (power == 0 && num!=0) { num2 = 1; } else if (power == 1) { num2 = num; }   else if (1 < power) { num2 = num * power(num, power -1); }   else if(0 > power) { num2 = 1/power(num, - power); } }```

This isn't quite correct (for various reasons).

1. This isn't proper Java syntax (no return type, num2 is never declared).
2. 0^0 = 1. In your code, as far as I can work out it's un-determined
3. Try running power(2,-1). You'll get a rather interesting result (correct result = 0.5). I actually can't think of a case off the top of my head where this code would give you correct results.
4. No non-integer inputs for power (or number, but this is easier to fix than non-integer powers), i.e. power(2.5, -1.346)
5. (minor issue) Assuming this code did actually work, this gives an O(n) solution at best (I didn't perform a thourough analysis, but it can only be worse). The best solution for computing x^y is O(1) (granted, there is a bit of "cheating" by using logarithm tables)
6. We're here to help people find the answers to their problems, not give the answer to them :P