Java Program to Check for Right Angled Triangle
source link: https://hackernoon.com/java-program-to-check-for-right-angled-triangle
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Java Program to Check for Right Angled Triangle
Java Program to Check for Right Angled Triangle
3 min
by @mayankvikash
Mayank Vikash
@mayankvikash
Student with some knowledge of how technologies work.
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.audio
element.Mayank Vikash
Student with some knowledge of how technologies work.
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");
}
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.
. . . comments & more!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK