Calculate the Power of a Number in Java

Exercise:

Write a Java Program to Calculate the Power of a Number.

1.Calculate using while loop

Click Here to View the Solution!
public class PowerProgram {
 
    public static void main(String[] args) {
 
        int base = 2, exponent = 8;
 
        long result = 1;
 
        while (exponent != 0)
        {
            result *= base;
            --exponent;
        }
 
        System.out.println("Answer = " + result);
    }
} 
Click Here to View the Output!
Answer = 256
Click Here to View the Explanation!
  • This program is used to calculate the power of a number by using a while loop.
  • Initially, two integer variables base and exponent are initialized as base = 2 and exponent = 8. And another long type variable result as result = 1.
  • The while loop will continue as long as the value of exponent remains not equal to zero.
  • In every iteration, the value of base will continue multiplying with the variable result and store the result in the variable result. And each time, the value of the exponent will decrement by one.
  • The loop will continue until the while condition becomes false (exponent = 0).
  • The value of base will therefore multiply 8 times with the result hence, result = 1*2*2*2*2*2*2*2*2 = 256. And will be printed on the screen.

2.Calculate using for loop

Click Here to View the Solution!
public class PowerCalculator {
 
    public static void main(String[] args) {
 
        int base = 2, exponent = 5;
 
        long result = 1;
 
        for (;exponent != 0; --exponent)
        {
            result *= base;
        }
 
        System.out.println("Answer = " + result);
    }
} 
Click Here to View the Output!
Answer = 32
Click Here to View the Explanation!
  • This program is used for calculating the power of a number by using a for loop and is printed.
  • The integer variables are initialized as the above program.
  • Unlike the while loop, the for loop will hold no initialization, a condition (exponent != 0) and an  expression that will update the value of exponent (--exponent).
  • In every iteration, the value of the exponent will decrement by one and the value of base will continue multiplying with the variable result and display the result.
  • This and the previous program work well with positive exponents however, they cannot execute using negative numbers. For this purpose a Java function pow() is used.

3.Calculate using pow() Function

Click Here to View the Solution!
 public class PowerCalculator {
 
    public static void main(String[] args) {
 
        int base = 2, exponent = -5;
        //Using built in function of pow() to calculate the power
        double result = Math.pow(base, exponent);
 
        System.out.println("Answer = " + result);
    }
} 
Click Here to View the Output!
Answer = 0.03125
Click Here to View the Explanation!
  • This program is used to calculate the power of a number by using the Java Math.pow() function.
  • The integers variable base and exponent are initialized as the previous programs.
  • The pow function is applied on both the base and exponent and stored in the variable result as follows, Math.pow(base, exponent).
  • The result is then displayed.