Java
Java

 

 

Introduction

In a right-angled triangle, the hypotenuse is the side opposite the right angle. The length of the hypotenuse
can be calculated using the well-known Pythagorean Theorem, which states that the square of the
length of the hypotenuse is equal to the sum of the squares of the lengths of the other two sides. The formula is:

        c² = a² + b²

Where:

  • c is the length of the hypotenuse
  • a and b are the lengths of the other two sides of the triangle

In this program, we will take the lengths of the two sides (a and b) from the user and calculate the hypotenuse (c).

Objective

The objective of this program is to calculate the length of the hypotenuse of a right-angled triangle using the Pythagorean Theorem.

Java Code

        import java.util.Scanner;

        public class HypotenuseCalculator {
            public static void main(String[] args) {
                // Create a Scanner object to take input from the user
                Scanner scanner = new Scanner(System.in);
                
                // Prompt the user to input the lengths of the two sides
                System.out.print("Enter the length of side a: ");
                double a = scanner.nextDouble();
                
                System.out.print("Enter the length of side b: ");
                double b = scanner.nextDouble();
                
                // Calculate the hypotenuse using the Pythagorean theorem
                double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
                
                // Output the result
                System.out.println("The length of the hypotenuse is: " + c);
                
                // Close the scanner to avoid resource leak
                scanner.close();
            }
        }

Program Explanation

This program is written in Java and uses the Scanner class to take input from the user for the
lengths of the two sides of the right-angled triangle. The program calculates the hypotenuse using the
Pythagorean Theorem formula (c² = a² + b²). It uses the Math.pow() method to square the values of a and b,
and Math.sqrt() to calculate the square root of the sum of these squares, which gives the length
of the hypotenuse.

How to Run the Program

  1. Open your favorite IDE (Integrated Development Environment) like Eclipse, IntelliJ, or NetBeans.
  2. Create a new Java project.
  3. Create a new Java class and copy the above code into the class.
  4. Run the program.
  5. The program will prompt you to enter the lengths of the two sides (a and b).
  6. After you enter the values, the program will display the length of the hypotenuse.
© 2024 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)