Write a program to input three sides of a triangle and check whether a triangle is possible or not. If possible, then display whether it is an Equilateral Triangle (All sides are Equal), an Isosceles Triangle (Any two sides are equal) or a Scalene Triangle (All three sides are unequal), else display “Triangle is not Possible”.
Answers
Required Answer:-
Question:
- Write a program to input three sides of a triangle and check whether triangle is possible or not. If possible, then display whether it is Equilateral, Isosceles or Scalene Triangle else display "Not possible."
Solution:
Here is the program.
import java.util.*;
public class IsTrianglePossible {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double a, b, c;
System.out.print("Enter first side: ");
a=sc.nextDouble();
System.out.print("Enter second side: ");
b=sc.nextDouble();
System.out.print("Enter third side: ");
c=sc.nextDouble();
if(a+b>c && b+c>a && a+c>b)
{
System.out.println("Triangle is possible.");
if(a==b && b==c)
System.out.println("Triangle is Equilateral Triangle.");
else if(a==b || b==c || c==a)
System.out.println("Triangle is Isosceles Triangle.");
else
System.out.println("Triangle is Scalene Triangle.");
}
else
System.out.println("Triangle not possible.");
sc.close();
}
}
Explanation:
- A triangle is said to be possible if sum of two sides is greater than the third side. For example, if the sides of a triangle are a, b and c, then a + b > c, b + c > a and a + c > b must be true. In this way, we can check if the triangle can be formed or not.
- Now, a triangle is said to be an equilateral triangle if all its sides are equal. Here we have checked that if a is equal to b and b is equal to c or not. If it's true then it is an equilateral.If it is an equilateral triangle, then a = b and b = c which implies that a = c. So, there is no need to add extra condition there.
- A triangle is said to be an isosceles triangle if two of its sides are equal and the third side is not equal to the length of other side. Here, we have checked if a = b or b = c or a = c. If its true, then it is an isosceles triangle.
- A triangle is said to be a scalene triangle if all its sides are unequal in length. If the triangle is neither an equilateral triangle nor an isosceles triangle, then it must be a scalene triangle.
Output is attached.
Explanation:
thank you . brainliast