Home >Java >javaTutorial >How to find the length of hypotenuse in Java?

How to find the length of hypotenuse in Java?

PHPz
PHPzforward
2023-09-09 22:33:05624browse

How to find the length of hypotenuse in Java?

The hypotenuse refers to the longest side of a right triangle opposite to the right angle.

You can use Pythagoras' theorem to find the length of the hypotenuse. According to the Pythagoras theorem, the sum of the squares of the lengths of two sides is equal to the square of the length of the third side, that is,

a2+ b2 = c2

where a, b, and c represent the three sides of a right triangle.

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

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

Show you some examples

The Chinese translation of

Instance-1

is:

Instance-1

Assume that the base length and height are 3 and 4 respectively.

Then by using the Pythagorean theorem formula,

Length of hypotenuse is 5
The Chinese translation of

Instance-2

is:

Instance-2

Assume that the base length and height are 8 and 10 respectively

Then by using the Pythagorean theorem formula,

Length of hypotenuse is 12.8
The Chinese translation of

Instance-3

is:

Instance-3

Assume that the base length and height are 6.5 and 8 respectively.

Then by using the Pythagorean theorem formula,

Length of hypotenuse is 10.3

grammar

To get the square root of a number, we can use the built-in sqrt() method in the Math class in the java.lang package.

The following is the syntax for using this method to obtain the square root of any number

double squareRoot = Math.sqrt(input_vale)

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

The following is the syntax for using this method to obtain the power of 2.

double power = Math.pow(inputValue,2)

The Math class of the java.lang package has a built-in method Math.hypot() which returns the square root of the sum of the squares of its arguments.

To get the length of the hypotenuse, we use the following formula

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

By using the Math.hypot() function, we can get the length of the hypotenuse.

Hypotenuse = Math.hypot(base, height)

algorithm

  • Step 1 - Obtain the lengths of the other two sides of the triangle, namely the height and the base, through initialization or user input.

  • Step 2 - Use the formula to calculate the length of the hypotenuse.

  • Step 3 - Print the results.

Multiple methods

We provide solutions in different ways.

  • By using the Pythagorean Theorem

  • By using the built-in Math.hypot() function

  • By using user-defined methods

Let’s look at the program and its output one by one.

Method 1: Use static input

In this method, the height and base length of the triangle will be initialized in the program. Then find the length of the hypotenuse by using the Pythagorean Theorem formula.

The Chinese translation of

Example

is:

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

Method 2: By using the value entered by the user

In this method, the height and base length of the triangle will be initialized in the program. Then use the built-in hypot() function to find the hypotenuse length.

The Chinese translation of

Example

is:

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

Method 3: Using user-defined methods

In this method, the height of the triangle and the length of the base will be initialized in the program. The length of the hypotenuse is then found by passing these values ​​as arguments and calling a user-defined method inside the method.

The Chinese translation of

Example

is:

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 the hypotenuse in Java by using different methods.

The above is the detailed content of How to find the length of hypotenuse in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete