Write a program that takes 3 numbers as input. If the 3 numbers form a right
angled triangle, print Right Triangle. You can answer in any programming
language like C, C++, Java, Python etc.
Answers
The given program is written in Java.
import java.util.*;
public class isRightTriangle{
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.print("Enter length of largest side: ");
a=sc.nextInt();
System.out.print("Enter length of base: ");
b=sc.nextInt();
System.out.print("Enter length of height: ");
c=sc.nextInt();
sc.close();
if(a * a == b * b + c * c)
System.out.println("\nIt is a Right angled triangle.");
else
System.out.println("\nIt is not a right angled triangle.");
}
}
- Accept the sides of the triangle.
- Check if the triangle satisfies pythagoras theorem.
- If true, triangle is a right angled triangle else not.
See the attachment for output.
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,c;
System.out.println("Enter the height of the triangle:");
a=in.nextInt();
System.out.println("Enter the base of the triangle:");
b=in.nextInt();
System.out.println("Enter the hypotenuse of the triangle");
c=in.nextInt(); if(a+b>c&&b+c>a&&c+a>b&&a-b<c&&b-c<a&&c-a<b)
{
System.out.println("Triangle is possible");
if(Math.sqrt(a*a+b*b)==(double)c)
System.out.println("It is a right angled triangle");
else
System.out.println("It is not a right angled triangle");
}
else
System.out.println("Triangle not possible");
}
}
Output is attached.