Get power using Math.pow in Java


In order to get the power of a number in Java, we use the java.lang.Math.pow() method. The Math.pow(double a, double b) method accepts two arguments of the double data type and returns a value of the first parameter raised to the power of the second argument.

Declaration - The java.lang.Math.pow() method is declared as follows −

public static double pow(double a, double b)

where a is the base and b is the power to which the base is raised.

Let us see a program where we find the power of a number using Math.pow() method

Example

 Live Demo

import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initializing some double values
      double x = 5.0;
      double y = 3.0;
      // computing the powers
      System.out.println( x + " raised to the power of " + y + " is " + Math.pow(x,y));
      System.out.println( y + " raised to the power of " + x + " is " + Math.pow(y,x));
   }
}

Output

5.0 raised to the power of 3.0 is 125.0
3.0 raised to the power of 5.0 is 243.0

Updated on: 26-Jun-2020

342 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements