Calculate power of given number in Java

In this tutorial we will learn writing a program in java to calculate the power of a given number. To calculate the power, user will give the base number and the exponent number. Power is calculated by multiplying the base number exponent times.

For Example:

Suppose we have to calculate 5 to power 3. Here 5 is base and 3 is exponent.

So 5^3 = 5 * 5 * 5 = 125 will be the output.

Power can be calculated using in built pow() method and using manual multiplications. In this tutorial we will see both ways.

Program 1: Calculate the power without using Pow() method in Java

In this program we are using a manual approach to calculating power through iterative multiplication. We are taking the value of base and exponent from the user. And the program performs the required multiplications to obtain the result.

import java.util.*;
public class Main
{
   	public static void main(String[] args) {
		int base,exponent,result=1;
		Scanner sc = new Scanner(System.in);
	      	System.out.println("Please give the base number");
	      	base= sc.nextInt();
	      	System.out.println("Please give the exponent number");
	      	exponent= sc.nextInt();
	      	System.out.println(base+" to the power "+exponent);
    		while (exponent != 0) {
        		 result = base * result;
       			 --exponent;
   		 }
   		 System.out.println(result);
	}
}

Output

Please give the base number
3
Please give the exponent number
5
3 to the power 5
243

Program 2: Java Program to calculate Power using pow() function

In this program we are using the built-in Math.pow method to calculate the power. Math.pow takes base and exponent as an input and return the result of after calculation the power.

import java.util.*;
public class Main
{
   	public static void main(String[] args) {
	int base,exponent;
	Scanner sc = new Scanner(System.in);
	System.out.println("Please give the base number");
	base= sc.nextInt();
	System.out.println("Please give the exponent number");
	exponent= sc.nextInt();
	System.out.println(base+" to the power "+exponent);
  	double result = Math.pow(base, exponent); 
   	System.out.println(result);
	}
}

Output

Please give the base number
5
Please give the exponent number
2
5 to the power 2
25.0

Program 3: Calculate Power using Java 8 Stream

import java.util.*;
import java.util.stream.*;
public class Main
{
   	public static void main(String[] args) {
	int base,exponent;
	Scanner sc = new Scanner(System.in);
	System.out.println("Please give the base number");
	base= sc.nextInt();
	System.out.println("Please give the exponent number");
	exponent= sc.nextInt();
	System.out.println(base+" to the power "+exponent);
  	double result = DoubleStream.of(base)
                .map(b -> Math.pow(b, exponent))
                .findFirst()
                .orElse(Double.NaN);
   	System.out.println(result);
	}
}

Output

Please give the base number
5
Please give the exponent number
3
5 to the power 3
125.0

Conclusion

In this tutorial we have learnt writing Java program to calculate the power of a given number. We have also explained the concept of power as the result of multiplying the base number by itself exponent times. We have explained two different approach in this tutorial. One using the built-in Math.pow method and another using the Math.pow function.

Through the above examples, you are now confident over writing java program to calculate the power. As we have explained multiple ways of achieving the same result by following different coding preferences and styles. The manual multiplication method give a clear understanding about the logic, while the use of Math.pow showcased more concise way to write a program.

I hope above tutorial helped you in writing and understanding power program in java.

Happy coding!