Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
math
pow
math.pow()

How to use the Math.pow() method in Java

Educative Answers Team

The Math.pow() is an built-in method in Java Math class and is used to calculate the power of a given number.

The power of a number refers to how many times to multiply the number with itself.

svg viewer

Method signature

The signature of the pow() method is as follows:

  • The method takes the base and exponent parameters of type double.
  • The method calculates multiplication of the base with itself exponent times and returns the result of type double.

See the following code snippet for example:

double pow(double base, double exponent)

Example

The pow() method is a part of java.lang.Math class, you need to import this class in your code to use the function.

Using the pow() method let’s calculate the following:

  • 545^4
  • 1.531.5^3
  • 50550^5
import java.lang.Math;

class CalculatePower {
    public static void main( String args[] ) {
      //Calculating 5^4 and casting the returned double result to int
      int ans1 = (int) Math.pow(5,4); 
      System.out.println("5^4 is "+ans1);
      //Calculating 1.5^3 and storing the returned double result to ans2
      double ans2 = Math.pow(1.5,3);
      System.out.println("1.5^3 is "+ans2);
      //Calculating 100^5 and casting the returned double result to int
      double ans3 = Math.pow(50,5);
      System.out.println("50^5 is "+ans3);
    }
}

RELATED TAGS

java
math
pow
math.pow()
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring