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
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
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)
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