In Java, to check if a triangle is equilateral, isosceles or scalene, we can use if-else-if conditions or the ternary Operator.
- If all three sides of a triangle are equal, it is classified as an equilateral triangle.
- If only two sides are equal, it is classified as an isosceles triangle.
- When none of the sides are equal, it is classified as a scalene triangle.
In this article, we are focusing on learning through examples, algorithm, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements(if-else and Switch)
- Ternary Operator
Problem Statement
We need to write a java program to check if a triangle is equilateral, isosceles or scalene. The program should take three sides as input and output the corresponding triangle. This above action should be done in our program. Let’s look at an example,
Example 1:
Input: 3 4 6
Scalene Triangle
Here, for the input sides 3,4,6, Output is “Scalene Triangle” as none of the triangle sides are equal.
Example 2:
Input: 3 3 3
Equilateral Triangle
Here, for the input sides 3, 3, 3.0, Output is “Equilateral Triangle” as all sides are equal.
Algorithm to Check a triangle is Equilateral, Isosceles or Scalene
- Start: Take three sides as input
- Condition Check:
- if(side1==side2 && side2==side3)
- It will print the result as triangle is Equilateral.
- if(side1==side2||side2==side3||side3==side1)
- it will print the result as triangle is Isosceles.
- When no sides are equal, then it is Scalene triangle.
- End: Now, Java will terminate the program.
Logic to Check a triangle is Equilateral, Isosceles or Scalene triangle
There are multiple logics to check a given input character is vowel or consonant in Java Programing language.
Logic 1: Using if-else-if
- After taking three sides as input from the user, we will check the conditions using if-else-if statements.
- If all three sides are equal, it prints that the triangle is Equilateral.
- Else, if the sides are not all equal, it will check if any two sides are equal.
- If any two sides are equal, it prints that the triangle is Isosceles.
- If none of the sides are equal, it prints that the triangle is Scalene.
if(side1==side2 && side2==side3)
/// all sides must equal
System.out.println("Equilateral triangle");
else if(side1==side2 || side2==side3||side3==side1)
//any 2 sides should be equal
System.out.println("Isosceles triangle");
else
//none of sides are equal
System.out.println("Scalene triangle");
Logic 2: Using Ternary Operator
- This operator takes three operands and works similarly to if-else statements. If the condition is true, it prints the first expression, if the condition is false, it prints the second expression.
- The program takes three input variables as sides and uses one more String variable to store the result. It stores ‘Equilateral triangle’ when all three sides are equal, ‘Isosceles triangle’ when only two sides are equal, and ‘Scalene triangle’ otherwise. The program then print the result variable to output the result.
String result;
result =(side1==side2)&&(side2==side3)? "Equilateral triangle":(side1==side2||side2==side3||side3==side2)?
"Isosceless triangle":"scalene triangle";
System.out.println(result);
Java Program to Check triangle is Equilateral, Isosceles or Scalene
Program 1: Using if-else-if
In the below Java program we are using if-else-if statement to check a triangle is Equilateral, Isosceles or Scalene triangle.
import java.util.Scanner;
public class TriangleChecking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the side 1 of a triangle");
int side1 = sc.nextInt();
System.out.println("Please enter the side 2 of a triangle");
int side2 = sc.nextInt();
System.out.println("Please enter the side 3 of a triangle");
int side3 = sc.nextInt();
if(side1==side2 && side2==side3)
/// all sides must equal
System.out.println("Equilateral triangle");
else if(side1==side2 || side2==side3||side3==side1)
//any 2 sides should be equal
System.out.println("Isosceles triangle");
else
//none of sides are equal
System.out.println("Scalene triangle");
sc.close();
}
}
Output:
Please enter the side 1 of a triangle
3
Please enter the side 2 of a triangle
4
Please enter the side 3 of a triangle
5
Scalene triangle
Explanation of above program
- This Java program checks the type of triangle based on the lengths of its sides.
- Asks the user to input the lengths of three sides of a triangle.
- It uses a Scanner object to read user input from the console.
- The lengths of the sides are stored in variables side1, side2, and side3.
- The program checks the conditions to determine the type of triangle based on the lengths of the sides.
- If all sides are equal (side1 == side2 == side3), it prints “Equilateral triangle”.
- Otherwise, If any two sides are equal (side1 == side2 or side2 == side3 or side3 == side1), it prints “Isosceles triangle”.
- If none of the sides are equal, it prints “Scalene triangle”.
- The Scanner is closed at the end of the program to release the resources it holds.
Program 2: Using Ternary Operator
In the below java program we are using Ternary Operator to check a triangle is Equilateral, Isosceles or Scalene triangle.
import java.util.Scanner;
public class TriangleChecking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the side 1 of a triangle");
int side1 = sc.nextInt();
System.out.println("Please enter the side 2 of a triangle");
int side2 = sc.nextInt();
System.out.println("Please enter the side 3 of a triangle");
int side3 = sc.nextInt();
String result;
result =(side1==side2)&&(side2==side3)? "Equilateral triangle":(side1==side2||side2==side3||side3==side2)?
"Isosceles triangle":"scalene triangle";
System.out.println(result);
sc.close();
}
}
Output:
Please enter the side 1 of a triangle
3
Please enter the side 2 of a triangle
3
Please enter the side 3 of a triangle
3
Equilateral triangle
Explanation of above program
- This Java program checks the type of triangle based on the lengths of its sides.
- Asks the user to input the lengths of three sides of a triangle.
- It uses a Scanner object to read user input from the console.
- The lengths of the sides are stored in variables side1, side2, and side3.
- It utilizes a ternary conditional operator to determine the type of triangle based on the lengths of the sides.
- If all sides are equal (side1 == side2 == side3), it assigns the string “Equilateral triangle” to the ‘result’ variable.
- Otherwise, If any two sides are equal (side1 == side2 or side2 == side3 or side3 == side1), it assigns the string “Isosceles triangle” to the ‘result’ variable.
- If none of the sides are equal, it assigns the string “Scalene triangle” to the ‘result’ variable.
- Finally, it prints the result.
- The Scanner is closed at the end of the program to release the resources it holds.
Conclusion
In this article, we explored how to check if a triangle is equilateral, isosceles or scalene using if-else-if statements and the ternary operator. We understood the logic behind this and moved on to writing the program.
For more information on related topics, you might find these articles helpful:
- Understanding the concepts of Conditional statements in Java
- Concepts of Operators in Java
- Top 10 Java Programming Tips
We hope you find this article helpful. If you have any questions or need any further clarifications, feel free to leave a comment. Don’t forget to subscribe our newsletter to get more updates on Java Programming Tips and tutorials.
Happy Programming!