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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad