Java Math pow() Function

pow() Function in Java

Java Math Class

Here, in the page we will discuss about the pow() Function in java.
Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), pow function etc.The java.lang.Math class contains various methods for performing basic numeric operations

Java pow Function :

Java Collection contains Math class and The class Math contains methods for performing several basic and advance numeric operations such as the exponential, logarithm, square root, and trigonometric functions.
The pow() method takes two arguments, say num1 and num2, and returns the result of the first argument i.e. num1, raised to the power of the second argument i.e. num2.
It is a static function of java math class and can be accessed using the math class.

Syntax:

Math.pow(number1,number2);

Definition of Parameters:

Return Type :

  • If the second argument i.e. num2 is zero, then the result is 1.
  • If the second argument i,e. num2 is 1, then the result is the same as the first argument i.e. num1.
  • If the second argument i.e. num2 is NaN, then the result is NaN.

Example :

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {
    double num1 = 9;
    double num2 = 2;
    
    // Storing the resultant
    double ans = Math.pow(num1,num2);
    
    // Printing the power
    System.out.println("Resultant Value : " + ans);
  }
}

Output:

Resultant Value : 81.0

Explanation:

In the above Example, We had taken two double values. The pow Function returns the result of the first argument i.e. num1, raised to the power of the second argument i.e. num2.

Example for Different Data Values:

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {
    // Integer variable
    double num1 = 5.0;
    double num2 = 3.0;

    // resultant value for Integer value
    System.out.println("Resultant value : " + Math.pow(num1, num2));

    // zero as argument
    double num = 0.0;
    System.out.println("Zero as Exponent : " + Math.pow(num1, num));
    System.out.println("Zero as Base : " + Math.pow(num, num2));

    // infinity as argument
    num = Double.POSITIVE_INFINITY;
    System.out.println("Infinity as Exponent : " + Math.pow(num1, num));
    System.out.println("Infinity as Base : " + Math.pow(num, num2));

  }
}

Output:

Resultant value : 125.0
Zero as Exponent : 1.0
Zero as Base : 0.0
Infinity as Exponent : Infinity
Infinity as Base : Infinity

Explanation:

In the above Example, We had taken two arguments of Integer and also zero and infinity value. The pow Function returns the result of the first argument i.e. num1, raised to the power of the second argument i.e. num2.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription