This article will outline various methods in which a number can be raised to the power of another number in java.

**Method 1: Using while loop**

Power of a number raised to another number is calculated by multiplying the number by itself for the same number of times as the power.

That is, if you want to calculate 2 raised to power of 3, then you should multiply 2 by itself for 3 times(same as the power to be raised).

Based on the above approach, java program to find power of a number can be written as

public class PowerCalculator { public static void main(String[] args) { // initialize values int power = 3; int number = 4; int result = 1; System.out.print(number +" raised to power " + power +" is : "); // loop till power is 0 while (power > 0) { // multiply number by itself result = result * number; // reduce loop counter power--; } System.out.print(result); } }

Output of the above program will be

4 raised to power 3 is : 64

Statement which multiplies the number by itself can be written in shorthand notation as

result *= number;

**Method 2: Using for loop**

This method uses the same approach as the earlier one except that it replaces the

`while`

loop with a `for`

loop. Modified program will be as below.public class PowerCalculator { public static void main(String[] args) { // initialize values int power = 3; int number = 4; int result = 1; System.out.print(number +" raised to power " + power +" is : "); // loop till power is 0 for(; power > 0; power--) { // multiply number by itself result = result * number; } System.out.print(result); } }

Note that the `for`

loop does not contain the initialization section since the variable is already initialized before the loop. Output remains the same.

**Method 3: Using recursion**

This method also follows the same approach as the above two methods but it replaces the loop with a recursive method.

A recursive method keeps on calling itself till some condition.

In order to calculate power of a number using recursive method, follow below steps.

- Create a method that accepts two int arguments, one representing the number or the base and second representing the power.
- This method should return an int which is the product of the number and the result of a call to this method with the value of power reduced by 1 in every call.
- This method should also check if the power is greater than 0 and return 1 if the power is 0. This is the terminating condition of recursive method. If it is not provided, then the recursive method will keep on running forever.

Java program written as per the above algorithm is given below.

public class PowerCalculator { public static void main(String[] args) throws IOException { // initialize values int power = 3; int number = 4; int result = calculatePower(4, 3); System.out.print(number + " raised to power " + power + " is : "); System.out.print(result); } /** * Recursive method to calculate power of a number * @param number * @param power * @return */ private static int calculatePower(int number, int power) { if (power == 0) { return 1; } // multiply the number with the result of the recursive call return number * calculatePower(number, --power); } }

From the program, it can be seen that the recursive method multiplies the number with the value returned by the recursive call to itself and in every call the power is reduced by 1.

Thus, when the power becomes 0, the recursive calls start returning and in turn multiply the number by itself for the number of times the method is called recursively, which is the same as the value of power.

Output of above program is

4 raised to power 3 is : 64

**Method 4: Using Math.pow function**

`java.lang.Math`

class has a `pow`

method which can calculate a number raised to the power of another. It takes two arguments with the first argument as the number and second as the power.It returns a

`double`

value which is the result of number raised to the power. Java program for this method is given below.public class PowerCalculator { public static void main(String[] args) { // initialize values int power = 3; int number = 4; int result = 1; int result = (int) Math.pow(number, power); System.out.print(number + " raised to power " + power + " is : "); System.out.print(result); } }

Output of above program will be

4 raised to power 3 is : 64

Note that the value returned by `Math.pow`

is casted to an `int`

. Without casting, the result will be in displayed with leading decimal, that is, 64.0.

Advantages of using `Math.pow`

over `for`

and `while`

loops are

- This method is shorter than the other two.
- It can be used with negative exponent, that is, you can raise a number to negative power.

**Hit the clap button**if the article was useful. 1