Programming Examples
Java program to find hypotenuse of a right angled triangle using the Pythagorean theorem
Write a Java program to calculate the hypotenuse of a right-angled triangle using the Pythagorean theorem:
where
- c is the hypotenuse.
- a and b are the lengths of the other two sides of the triangle.
Solution
import java.util.Scanner;
public class HypotenuseCalculator
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of side a: ");
double a = sc.nextDouble();
System.out.print("Enter the length of side b: ");
double b = sc.nextDouble();
double hypotenuse = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
System.out.println("The length of the hypotenuse is: " + hypotenuse);
}
}
Output