➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
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) | e0 | 1.0 |
Math.exp(1) | e1 | 2.718281828459045 |
Math.exp(2) | e2 | 7.38905609893065 |
Math.exp(3) | e3 | 20.085536923187668 |
Math.exp(4) | e4 | 54.598150033144236 |
Math.exp(5) | e5 | 148.4131591025766 |
Math.exp(6) | e6 | 403.4287934927351 |
Math.exp(7) | e7 | 1096.6331584284585 |
Math.exp(8) | e8 | 2980.9579870417283 |
Math.exp(9) | e9 | 8103.083927575384 |
Math.exp(10) | e10 | 22026.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!