Write a java program to check whether the triangle is equilateral, isosceles or scalene triangle.

Last update on May 21 2022 13:09:25 (UTC/GMT +8 hours)

Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.

Equilateral triangle: An equilateral triangle is a triangle in which all three sides are equal. In the familiar Euclidean geometry, equilateral triangles are also equiangular; that is, all three internal angles are also congruent to each other and are each 60°.

Isosceles triangle: An isosceles triangle is a triangle that has two sides of equal length.

Scalene triangle: A scalene triangle is a triangle that has three unequal sides, such as those illustrated above.

Pictorial Presentation:

Write a java program to check whether the triangle is equilateral, isosceles or scalene triangle.

Sample Solution:

C Code:

#include <stdio.h> int main() { int sidea, sideb, sidec; //are three sides of a triangle /* * Reads all sides of a triangle */ printf("Input three sides of triangle: "); scanf("%d %d %d", &sidea, &sideb, &sidec); if(sidea==sideb && sideb==sidec) //check whether all sides are equal { printf("This is an equilateral triangle.\n"); } else if(sidea==sideb || sidea==sidec || sideb==sidec) //check whether two sides are equal { printf("This is an isosceles triangle.\n"); } else //check whether no sides are equal { printf("This is a scalene triangle.\n"); } return 0; }

Sample Output:

Input three sides of triangle: 50 50 60 This is an isosceles triangle.

Flowchart:

Write a java program to check whether the triangle is equilateral, isosceles or scalene triangle.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a C program to read temperature in centigrade and display a suitable message according to temperature state below.
Next: Write a C program to check whether a triangle can be formed by the given value for the angles.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Maximum value of int:

In C:

#include <limits.h> then use int imin = INT_MIN; // minimum value int imax = INT_MAX;

or

#include <float.h> float fmin = FLT_MIN; // minimum positive value double dmin = DBL_MIN; // minimum positive value float fmax = FLT_MAX; double dmax = DBL_MAX;

Ref : https://bit.ly/3fi8yk9

I am trying to write java program to see if a triangle is scalene, isosceles, equilateral or not a triangle. With the integers I used it is supposed to be not a triangle (1, 1, 30). But I keep getting scalene and not a triangle together. Any help is appreciated! Thank you!

public class Tri { static void checkTriangle(int x, int y, int z) { // Check for equilateral triangle if (x == y && y == z ) System.out.println("Equilateral Triangle"); // Check for isoceles triangle else if (x == y || y == z || z == x ) System.out.println("Isoceles Triangle"); // Check for scalene triangle else if (x != y || y!= z || z != x) System.out.println("Scalene Triangle"); { // Check for not a triangle if (x + y < z || x + z < y || y + z > x) System.out.println("Not a triangle"); } } public static void main(String[] args) { { int x = 1, y = 1, z = 30; checkTriangle(x, y, z); } } }

Given the sides of a triangle, write a program to check whether the triangle is equilateral, isosceles or scalene and find its area.

import java.util.Scanner; class Triangle { public static void main(String args[]) { // Scanner class object to read input values Scanner sc = new Scanner(System.in); // declare variables int a, b, c; double p, area; // read sides of trangle from user System.out.print("Enter 3 sides : "); a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); //check is sides can form a triangle if ((a < b + c) && (b < a + c) && (c < a + b)) { // equilateral triangle if all sides are equal if ((a == b) && (b == c)) System.out.println("Equilateral triangle"); // isosceles triangle if any two sides are equal else if ((a == b) || (b == c) || (c == a)) System.out.println("Isosceles triangle"); // scalene triangle if no sides are equal else System.out.println("Scalene triangle"); // find area p = (a + b + c) / 2; area = Math.sqrt(p * (p - a) * (p - b) * (p - c)); // display area System.out.println("Area of triangle is = " + area); } else { System.out.println("Cannot form a triangle"); } } }Enter 3 sides : 3 4 5 Scalene triangle Area of triangle is = 6.0

Write a java program to check whether the triangle is equilateral, isosceles or scalene triangle.
report this ad

Write a program to input the three sides of a triangle and check whether it forms a triangle or not, if it forms a triangle, check whether it is an equilateral, isosceles or a scalene triangle.

(Hint: To form a triangle, each side should be less the sum of the other two sides.

To form an equilateral triangle every side should be equal.

To form an isosceles triangle any two sides should be equal.

To form a scalene triangle all three sides should be different from each other.)

Given three integers as X, Y, and Z representing the three sides of a triangle, the task is to check whether the triangle formed by the given sides is equilateral, isosceles, or scalene.

Equilateral Triangle: A triangle is said to be equilateral triangle if all the sides are equal. If X, Y, Z are three sides of the triangle. Then, the triangle is equilateral only if X = Y = Z.

Isosceles Triangle: A triangle is said to be an isosceles triangle if any of its two sides are equal. If X, Y, Z are three sides of the triangle.Then, the triangle is isosceles if either X = Y or X = Z or Y = Z.

Scalene Triangle: A triangle is said Scalene Triangle if none of its sides is equal.

Examples:



Input: X = 6, Y = 8, Z = 10
Output: Scalene Triangle
Explanation:
Since all the sides of the given triangle are unequal, the triangle is scalene.

Input: X = 10, Y = 10, Z = 10
Output: Equilateral Triangle
Explanation:
Since all the sides of the given triangle are equal.

Approach: Follow the steps below to solve the problem:

  1. Check if X = Y and Y = Z. If found to be true, print “Equilateral Triangle”.
  2. If it is not an Equilateral triangle, then check if X = Y or X = Z or Y = Z. If found to be true, print “Isosceles Triangle”.
  3. If none of the above steps are satisfied, then print “Scalene Triangle”.

Below is the implementation of the above approach:

void checkTriangle(int x, int y, int z)

        cout << "Equilateral Triangle";

    else if (x == y || y == z || z == x)

        cout << "Isosceles Triangle";

        cout << "Scalene Triangle";

static void checkTriangle(int x, int y, int z)

        System.out.println("Equilateral Triangle");

    else if (x == y || y == z || z == x )

        System.out.println("Isosceles Triangle");

        System.out.println("Scalene Triangle");

public static void main(String[] args)

def checkTriangle(x, y, z):

        print("Equilateral Triangle")

    elif x == y or y == z or z == x:

        print("Isosceles Triangle")

        print("Scalene Triangle")

static void checkTriangle(int x, int y, int z)

        Console.WriteLine("Equilateral Triangle");

    else if (x == y || y == z || z == x )

        Console.WriteLine("Isosceles Triangle");

        Console.WriteLine("Scalene Triangle");

public static void Main()

function checkTriangle(x, y, z)

        document.write("Equilateral Triangle");

    else if (x == y || y == z || z == x)

        document.write("Isosceles Triangle");

        document.write("Scalene Triangle");

Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :

Practice Tags :