paint-brush
Java Program to Check for Right Angled Triangleby@mayankvikash
10,610 reads
10,610 reads

Java Program to Check for Right Angled Triangle

by Mayank VikashOctober 21st, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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. 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.
featured image - Java Program to Check for Right Angled Triangle
Mayank Vikash HackerNoon profile picture


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:


Screenshot of the program

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

Not a right-angled triangle


Right-angled triangle


Also published here.