Programming Examples
Java program to accept three sides of a triangle and check whether it is an acute angled obtuse angled or right angled triangle
Write a program to accept three sides of a triangle as parameter and check whether it can form a triangle or not. If it forms a triangle, check whether it is an acute angled, obtuse angled or right-angled triangle.
(Hint: To form a triangle, each side should be less the sum of the other two sides..
To form an acute angled triangle the square of every side should be less than the sum of the squares of the other two sides.
To form an obtuse angled triangle the square of any side should be greater than the sum of the squares of the other two sides.
To form an right angled triangle the square of any side should be equal to the sum of the squares of the other two sides.)
Solution
import java.util.*;
class Triangle
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter 3 sides:”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a<b+c && b<a+c && c<a+b)
{
if(a*a<b*b+c*c && b*b<a*a+c*c && c*c<a*a+b*b)
System.out.println(“Acute angled triangle”);
else if(a*a>b*b+c*c || b*b>a*a+c*c || c*c>a*a+b*b)
System.out.println(“Obtuse angled triangle”);
else
System.out.println(“Right angled triangle”);
}
else
System.out.println(“Cannot form a triangle”);
}
}
Output