How to Find Exponents in Java

How to find exponents in Java | To find exponents value in Java, we can use the pow() method of java.lang.Math class. But if you want to find ex value (where e is Euler’s number) then use the exp() method. Here we will discuss both of them.

Find exponents in Java using Math.pow() method

The pow() method accepts two double type arguments, and returns the value of the first argument raised to the power of the second argument.

Method prototype of java.lang.Math.pow() is,
public static double pow(double a, double b)

public class Test{
   public static void main(String[] args) {
      System.out.println(Math.pow(2, 3));
   }
}

Program:- Java program to find area of a circle.

public class CircleArea{
   public static void main(String[] args) {
      int radius = 10;

      // circle area = π * r * r
      double area = Math.PI * Math.pow(radius, 2);

      // display area
      System.out.println(area);
   }
}

Output:-

314.1592653589793

If we import Math class statically and then we can invoke pow() method without calling through it’s class name.

import static java.lang.Math.*;
public class Test{
   public static void main(String[] args) {
      System.out.println(pow(2, 3)); // 8
   }
}

The “import static java.lang.Math.*;” statement will import all static members of the Math class. But if we want to import only the sqrt() method of the Math class, not another static method and variables of Math class then we can use the “import static java.lang.Math.sqrt;” statement. Learn more about static import in Java

import static java.lang.Math.pow;
public class Test{
   public static void main(String[] args) {
      System.out.println(pow(2, 3)); // 8
   }
}

See special cases for Math.pow() method.

Math.exp() method

The exp() method of java.lang.Math class returns Euler’s number (e) raised to the power of a double value.

Mathod prototype of java.lang.Math.exp(),
public static double exp(double a)

If the argument value:- a
then return value:- ea

Example:-

public class Test{
   public static void main(String[] args) {
      System.out.println(Math.exp(0));
      System.out.println(Math.exp(1));
      System.out.println(Math.exp(2));
   }
}

Output:-

1.0
2.718281828459045
7.38905609893065

Math.exp(0)e01.0
Math.exp(1)e12.718281828459045
Math.exp(2)e27.38905609893065
Math.exp(3)e320.085536923187668
Math.exp(4)e454.598150033144236
Math.exp(5)e5148.4131591025766
Math.exp(6)e6403.4287934927351
Math.exp(7)e71096.6331584284585
Math.exp(8)e82980.9579870417283
Math.exp(9)e98103.083927575384
Math.exp(10)e1022026.465794806718

Special cases:

  • If the argument is NaN, the result is NaN.
  • Or If the argument is positive infinity, then the result is positive infinity.
  • If the argument is negative infinity, then the result is positive zero.
public class Test{
   public static void main(String[] args) {
      System.out.println(Math.exp(Double.NaN));
      System.out.println(Math.exp(Double.POSITIVE_INFINITY));
      System.out.println(Math.exp(Double.NEGATIVE_INFINITY));
   }
}

Output:-

NaN
Infinity
0.0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *