Java Math hypot() Method

Java Math hypot() Method

Java Math Class

Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.
The java.lang.Math class contains various methods for performing basic numeric operations

Here, in the page we will discuss about the math hypot() Method in java.

Java Math hypot() Method :

The hypot() method in Java is a static method that belongs to the Math class. It returns the square root of the sum of the squares of its two arguments, without intermediate overflow or underflow. It can be used to calculate the length of the hypotenuse of a right triangle, given the lengths of the other two sides.

Special cases:

  • If either argument is infinite, then the result is positive infinity.
  • If either argument is NaN and neither argument is infinite, then the result is NaN.

Syntax  :

public static double hypot(double x,double y)

Example 1 :  

Run
public class Main {
    public static void main(String[] args) {
        double side1 = 3.0;
        double side2 = 4.0;
        double hypotenuse = Math.hypot(side1, side2);

        System.out.println("The length of the hypotenuse is: " + hypotenuse);
    }
}

Output :

The length of the hypotenuse is: 5.0

Explanation :

This program calculates the length of the hypotenuse of a right triangle using the hypot() method, given the lengths of the other two sides. The hypot() method returns the square root of the sum of the squares of its two arguments, which is the formula for calculating the length of the hypotenuse of a right triangle.

Example 2:  

Run
public class Main {
    public static void main(String[] args) {
        double side1 = 10.0;
        double side2 = 20.0;
        double hypotenuse = Math.hypot(side1, side2);

        System.out.println("The length of the hypotenuse is: " + hypotenuse);
    }
}

Output :

The length of the hypotenuse is: 22.360679774997898

Explanation :

In this example, The hypot() method takes two arguments, which are the lengths of the two sides of the triangle. It returns the square root of the sum of the squares of these two arguments. This is the formula for calculating the length of the hypotenuse of a right triangle, using the Pythagorean theorem.

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