Calculate Power of a number using pow() in Java

In this tutorial we will learn writing Java Program to calculate the power for the given base and exponents using Math.pow() method. If user enters base as 3 and exponent as 4, output should be 81.

We will also learn with example, detailed logic and program explanation for better understanding.

Required Knowledge

Problem Statement

We need to write a program to calculate power for the given base and exponents using Math.pow() method. The program should take base and exponent values from the user and should calculate power. Let’s see an example,

Example

For input base 2 and exponent 3, output should be 8.

Explanation:

Input integer base = 2

Input integer exponent = 3

result =(int)Math.pow(base, exponent) = (int)Math.pow(2,3) = 2^3 = 8.

Hence, for input base 2 and exponent 3, we will get the power of a number as result 8.

Logic to calculate power of a number using pow method

  • Take an integer value as base and another integer value as a exponent from the user.
  • The Math.pow(base, exponent) expression calculates the base raised to the power of the exponent.
  • Since, the Math.pow(base, exponent) method returns a double, the result is cast to an integer (int) and stored in the result variable.
  • Program will print this result variable to display the output.

Java program to Calculate power of a number using pow() method

import java.util.Scanner;
public class PowerUsingPowMethod {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Please enter base");
		int base = s.nextInt();
		System.out.println("Please enter exponent");
		int exponent = s.nextInt();
		int result= (int)Math.pow(base, exponent);
		System.out.println("Result is: "+result);
		s.close();
	}
}

Output:

Please enter base
35
Please enter exponent
3
Result is: 42875

Java program explanation to calculate power of a number using pow method

  1. The Java program begins by asking the user to input values for the base and exponent.
  2. Using a Scanner object, it reads and captures these inputs.
  3. The program employs the Math.pow() method to compute the result of raising the base to the exponent.
  4. This computed result is stored in a variable named result.
  5. Subsequently, the program outputs the calculated result to the console.
  6. Finally, it ensures proper resource management by closing the Scanner object.

Conclusion

In this article, we have learnt writing java program to calculate power of a number using pow() method. We have understood the logic and seen the program using pow() method in Java. Hope this tutorial helped you in the understanding of the program.

You can also check our another awesome tutorials

  1. Check perfect number in Java
  2. Java program to calculate power without using pow() method
  3. Program to find strong numbers in a given range in Java
  4. Java program to count the digits of a given number

Happy Coding!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.