Java program to calculate the hypotenuse of a right triangle


How to find the length of the hypotenuse in Java

This program uses the Pythagoras theorem to calculate the length of the hypotenuse of a right triangle.

The Pythagorean theorem states that the square of the length of the hypotenuse equals the sum of the squares of the lengths of the other two sides of the triangle.






We use this Math Class methods:

Math.sqrt()  to calculate the square root
Math.pow() to raise each side of the triangle to the power of 2

The program asks the user to input the length of base (side 1) and perpendicular (side2) and calculates the hypotenuse using this statement according to the Pythagoras' theorem:

hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(perpendicular, 2));

Java program to calculate hypotenuse of a right angled triangle
import java.util.Scanner;

public class JavaApplication359 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double base, perpendicular, hypotenuse;         

        //get the length of the base
        System.out.print("Enter a value for base: ");                                                             
        base = input.nextDouble();

        //get the length of the perpendicular
        System.out.print("Enter a value for perpendicular: ");
        perpendicular = input.nextDouble();

        //find the square root of (base^2 + perpendicular^2)
        hypotenuse = Math.sqrt(Math.pow(base, 2) + Math.pow(perpendicular, 2));

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

    }
}

9 comments :

  1. how to study easy java program

    ReplyDelete
    Replies
    1. By understanding the functions clearly one at a time, and then trying small programs in your own...slowly you will be able to make out these larger programs.. needs time and practice only ...

      Delete
  2. Why 395 is given there? In line no.

    ReplyDelete
  3. Replies
    1. ade class name ra kukka yadava, ayna avanni nek enduku be

      Delete
  4. Write with base and perpidecular please

    ReplyDelete