Java Program to Check for Right Angled Triangle

Written by mayankvikash | Published 2022/10/21
Tech Story Tags: java | learning-to-code | tutorial | mathematics-and-programming | mathematics | programming | java-programming | learn-java

TLDRA right-angled triangle is a triangle with one of its interior angles equal to 90 degrees or any one angle is a right angle. Pythagoras’s Theorem is mentioned in the Baudhayana Sulba-sutra of India, which was written between 800 and 400 BCE. The square of the hypotenuse is equal to the sum of the squared of the squares of the other two sides of a triangle. If the square is equal, the triangle is left-angled.via the TL;DR App

A right-angled triangle is a triangle with one of its interior angles equal to 90 degrees or any one angle is a right angle.

There are several properties of a right-angled triangle; one of them is that the square of the hypotenuse is equal to the sum of the square of the perpendicular and base of a triangle. This is called the Pythagoras Theorem.

The hypotenuse is the longest side of a triangle.

Fun Fact: Pythagoras’s Theorem was invented in India, long before Pythagoras was even born.

The theorem is mentioned in the Baudhayana Sulba-sutra of India, which was written between 800 and 400 BCE.

Program’s Logic

According to the theorem, the squared of Hypotenuse is equal to the sum of the squared of the other two sides. So, we have to take the input of the two sides of a triangle and a hypotenuse. Calculate the sum of squares of the other two sides and if the sum is equal to the square of the hypotenuse, the triangle is right-angled.

Program’s Screenshot:

Let’s write the program

Basic program structure:

import java.util*;
public class RightAngledTriangle{
  public static void main(String args[]){
    Scanner in = new Scanner(System.in);
    // code
  }
}

Declaring variables:

int h, p, b;

Asking the user for input:

System.out.println("Enter the Hypotenuse");
h = in.nextInt();
System.out.println("Enter the Perpendicular");
p = in.nextInt();
System.out.println("Enter the Base");
b = in.nextInt();

The simple way to check if the squared of the hypotenuse is equal to the sum of the squared of the perpendicular and base is to use if-else.

If-else condition:

if (h*h==(p*p)+(b*b)){
    System.out.println("Right Angled Triangle");
}
else{
    System.out.println("Not a right angled Traingle");
}

Code

import java.util.*;
public class RightAngledTriangle {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int h, p, b;
        System.out.println("Enter the Hypotenuse");
        h = in.nextInt();
        System.out.println("Enter the Perpendicular");
        p = in.nextInt();
        System.out.println("Enter the Base");
        b = in.nextInt();

        if (h*h==(p*p)+(b*b)){
            System.out.println("Right Angled Triangle");
        }
        else{
            System.out.println("Not a right angled Traingle");
        }

    }
}

Output


Also published here.


Written by mayankvikash | Student with interest in tech
Published by HackerNoon on 2022/10/21