Java Programming
________________
Find different types of triangles using if else if.
You can take different types of triangles as Equilateral, scalene and isosceles.
Answers
The given problem is solved using language - Java.
import java.util.Scanner;
public class Triangle{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
double a,b,c;
System.out.print("Enter first side of triangle: ");
a=sc.nextDouble();
System.out.print("Enter second side of triangle: ");
b=sc.nextDouble();
System.out.print("Enter third side of triangle: ");
c=sc.nextDouble();
sc.close();
if(a+b>c&&b+c>a&&c+a>b){
if(a==b&&b==c)
System.out.println("Equilateral Triangle.");
else if(a==b||b==c||c==a)
System.out.println("Isosceles Triangle.");
else
System.out.println("Scalene Triangle.");
}
else
System.out.println("Triangle Not Possible.");
}
}
- At first, check whether the triangle is possible or not. A triangle is possible when sum of two sides is greater than the other side.
- Now, check if all the sides of triangle are equal or not. If true, triangle is Equilateral.
- If false, check if any two sides are equal or not. If true, the triangle is Isosceles.
- If all the conditions are false, the given triangle is Scalene.
See the attachments for output.
Explanation:
Solution
:
The given problem is solved using language - Java.
import java.util.Scanner;
public class Triangle{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
double a,b,c;
System.out.print("Enter first side of triangle: ");
a=sc.nextDouble();
System.out.print("Enter second side of triangle: ");
b=sc.nextDouble();
System.out.print("Enter third side of triangle: ");
c=sc.nextDouble();
sc.close();
if(a+b>c&&b+c>a&&c+a>b){
if(a==b&&b==c)
System.out.println("Equilateral Triangle.");
else if(a==b||b==c||c==a)
System.out.println("Isosceles Triangle.");
else
System.out.println("Scalene Triangle.");
}
else
System.out.println("Triangle Not Possible.");
}
}
Logic
:
At first, check whether the triangle is possible or not. A triangle is possible when sum of two sides is greater than the other side.
Now, check if all the sides of triangle are equal or not. If true, triangle is Equilateral.
If false, check if any two sides are equal or not. If true, the triangle is Isosceles.
If all the conditions are false, the given triangle is Scalene.
See the attachments for output.