ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to calculate hypotenuse of a right angled triangle in Java ?.

Updated on September 17, 2014

Introduction

In this Java tutorial, you will learn a simple Java programming tutorial, which calculates hypotenuse of right angle triangle in Java.

Formula used - hypotenuse * hypotenuse = base * base + height * height.

In this Java tutorial, below section has a Java video tutorial which demonstrate how to calculate hypotenuse of right angled triangle in Java. Following to Java video tutorial there is complete source code for hypotenuse calculation and its working. In the last section of tutorial there is output of the Java program.

Formula - Hypotenuse of right angled triangle

In the below capsule, there is an image which depicts a right angled triangle. It shows base , height and hypotenuse of a right angled triangle. In order to calculate hypotenuse of right angled triangle formula used is -

hypotenuse * hypotenuse = base * base + height * height.

Java video tutorial - How to calculate hypotenuse of a right angled triangle in Java ?.

Below is the video tutorial which demonstrates how to calculate hypotenuse of a right angled triangle in Java. It provides great description and source code demo in Eclipse workspace editor. The video tutorial provides end-to-end demo for hypotenuse calculation.

Eclipse snapshot of Java program -

Below is the snapshot of Eclipse workspace editor, it shows Java source code used to calculate hypotenuse of right angled triangle in Java.

How it works -

Step 1 - In this Java code, create a Scanner object which takes System.in instance. System.in instance helps scanner class to read inputs from the console. It is inputstream which listens to Java console.

Step 2 - create a height variable to hold height of right angled triangle.

Step 3 - Prompt the user to enter height of right angled triangle.

Step 4 - Scanner class has method nextDouble. It reads inputs entered by user on the console and stores the double value in height variable.

Step 5 - create a base variable to hold base of right angled triangle.

Step 6 - Prompt the user to enter base of right angled triangle.

Step 7 - Scanner class has method nextDouble. It reads inputs entered by user on the console and stores the double value in base variable.

Step 8 - create a double variable by name hypotenuse. It will hold hypotenuse value of right angled triangle.

Step 9 - Math class has a static method sqrt(). It calculates the square root of the argument passed to it. Math class also has pow() method which is static method. It takes two arguments and returns value of the first argument raised to the power of the second argument.

Step 10 - Printing hypotenuse of right angled triangle on Java console.

Java source code - how to calculate hypotenuse of right angled triangle in Java.

In following section, you are going to encounter source code to calculate hypotenuse of right angled triangle in Java.

import java.util.Scanner;

public class HypotenuseDemo {

	public static void main(String[] args) {

		// Step 1 - In this Java code, create a Scanner object
		// which takes System.in instance. System.in instance helps
		// scanner class to read inputs from the console. It is 
		// inputstream which listens to Java console.
		Scanner input = new Scanner(System.in);

		// Step 2 - create a height variable to hold height of 
		// right angled triangle.
		double height = 0;

		// Step 3 - Prompt the user to enter height of right angled triangle.
		System.out.print("Enter height of right angled triangle : ");

		// Step 4 - Scanner class has method nextDouble. It reads inputs 
		// entered by user on the console and stores the double value in 
		// height variable.
		height = input.nextDouble();  

		// Step 5 - create a base variable to hold base of 
		// right angled triangle. 
		double base = 0;

		// Step 6 - Prompt the user to enter base of right angled triangle.
		System.out.print("Enter base of right-angled triangle : ");

		// Step 7 - Scanner class has method nextDouble. It reads inputs 
		// entered by user on the console and stores the double value in 
		// base variable.
		base = input.nextDouble();

		// Step 8 - create a double variable by name hypotenuse. It will
		// hold hypotenuse value of right angled triangle.
		double hypotenuse = 0;

		// Step 9 - Math class has a static method sqrt(). It calculates 
		// the square root of the argument passed to it.
		// Math class also has pow() method which is static method. It
		// takes two arguments and returns value of the first argument 
		// raised to the power of the second argument.

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

		// Step 10 - Printing hypotenuse of right angled triangle on 
		// Java console.
		System.out.println("Hypotenuse of right-angled triangle : " 
				+ hypotenuse);
	}
}




Output of the program -

In the following section, you will encounter a image of Eclipse Java console. This image shows the output of the above Java program.

Enter height of right angled triangle : 3
Enter base of right-angled triangle : 4
Hypotenuse of right-angled triangle : 5.0

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article