How To Find the Length of Hypotenuse in Java?


Hypotenuse refers to the longest side which is opposite to the right angle of a right-angled triangle.

The length of the hypotenuse can be found using Pythagorean theorem. According to Pythagoras theorem, sum of square of length of two sides is equal to the of square of length of third side i.e,

a2+ b2 = c2

Where, a, b and c refer to the three sides of the right-angled triangle.

So, Hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2))

In this article, we will see how we can find the length of hypotenuse by using Java programming language.

To show you some instances

Instance-1

Suppose length of base and height are 3 and 4 respectively.

Then by using Pythagoras formula,

Length of hypotenuse is 5

Instance-2

Suppose length of base and height are 8 and 10 respectively

Then by using Pythagoras formula,

Length of hypotenuse is 12.8

Instance-3

Suppose length of base and height are 6.5 and 8 respectively.

Then by using Pythagoras formula,

Length of hypotenuse is 10.3

Syntax

To get the square root of a number we have inbuilt sqrt() method in the Math class of java.lang package.

Following is the syntax to get square root of any number by using the method

double squareRoot = Math.sqrt(input_vale)

Similarly, to get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the method.

double power = Math.pow(inputValue,2)

Math class of the java.lang package has an inbuilt method Math.hypot() which returns the square root of the sum of squares of its arguments.

To get length of hypotenuse we are using the below formula

Hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2))

Where by using Math.hypot() function we can get the length of hypotenuse.

Hypotenuse = Math.hypot(base, height)

Algorithm

  • Step 1 − Get the length of other two sides i.e. height and base of the triangle either by initialization or by user input.

  • Step 2 − Find the length of the hypotenuse by using the formula.

  • Step 3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Pythagorean Theorem

  • By Using Inbuilt Math.hypot() Function

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input

In this approach, the length of height and base of the triangle will be initialized in the program. Then by using the Pythagoras theorem formula, find the hypotenuse length.

Example

import java.util.*; public class Main { public static void main(String[] args) { //initialized length of the base double base = 10; System.out.println("Given length of base: "+base); //initialized length of the height double height = 20; System.out.println("Given length of height: "+height); //find the length of hypotenuse by using Pythagoras formula double hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(height, 2)); //print the result System.out.println("Length of the hypotenuse: " + hypotenuse); } }

Output

Given length of base: 10.0
Given length of height: 20.0
Length of the hypotenuse: 22.360679774997898

Approach-2: By Using User Input Value

In this approach, the length of height and base of the triangle will be initialized in the program. Then by using the inbuilt hypot() function, find the hypotenuse length.

Example

import java.util.*; public class Main { public static void main(String[] args) { //initialized length of the base double base = 15; System.out.println("Given length of base: "+base); //initialized length of the height double height = 20; System.out.println("Given length of height: "+height); //find the length of hypotenuse by using Math.hypot() method double hypotenuse = Math.hypot(base, height); //print the result System.out.println("Length of the hypotenuse: " + hypotenuse); } }

Output

Given length of base: 15.0
Given length of height: 20.0
Length of the hypotenuse: 25.0

Approach-3: By Using User Defined Method

In this approach, the length of height and base of the triangle will be initialized in the program. Then call a user defined method by passing these values as parameters and inside the method find the hypotenuse length.

Example

import java.util.*; public class Main { public static void main(String[] args) { //initialized length of the base double base = 12; System.out.println("Given length of base: "+base); //initialized length of the height double height = 18; System.out.println("Given length of height: "+height); //calling the user defined method hypotenuseLength(base,height); } //find length of hypotenuse public static void hypotenuseLength(double base, double height) { //find the length of hypotenuse by using Math.hypot() method double hypotenuse = Math.hypot(base, height); //print the result System.out.println("Length of the hypotenuse: " + hypotenuse); } }

Output

Given length of base: 12.0
Given length of height: 18.0
Length of the hypotenuse: 21.633307652783937

In this article, we explored how to find the length of hypotenuse in Java by using different approaches.

Updated on: 17-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements