C Program To Check whether Triangle is Equilateral, Isosceles or Scalene

The program determines the type of triangle based on the input provided by a user.

A triangle can be broadly classified according to the length of its sides and the angle between the sides.
Based on the sides, there are three types of triangle
1. Equilateral Triangle
2. Isosceles Triangle
3. Scalene Triangle

Based on the angles, there are also 3 types:
1. Acute triangle
2. Right triangle
3. Obtuse triangle

In this article, we’ll be discussing triangles that are classified based on sides, and implementing the same in the program.

type of triangle

  1. Equilateral Triangle: A triangle in which all sides are of the same length
  2. Isosceles Triangle: A triangle in which exactly two sides are equal
  3. Scalene Triangle: A triangle in which all sides are having different length

Approach to determine the type of a triangle

  1. Read the length of a triangle for the user
  2. If side1, side2 and side3 are equal to each other, then
    • print a message the triangle is an equilateral triangle
  3.  If any two from side1, side2 or side3 are having the same length, then
    • print message the triangle is isosceles triangle
  4.  If all three sides are different in length, then
    • print message the triangle is a scalene triangle

C Program To Check whether Triangle is Equilateral, Isosceles or Scalene

The program also implies Triangle Inequality Theorem to validate whether the given sides can be a triangle or not.

Program:

#include<conio.h>
#include<stdio.h>
#include<math.h>
void
main ()
{
  int a, b, c, flag = -1;
  printf (" Enter the values of a, b and c : = ");
  scanf ("%d %d %d", &a, &b, &c);
  if ((a >= 0 && a <= 10) && (b >= 0 && b <= 10) && (c >= 0 && c <= 10)){
 
 // Triangle Inequality Theorem : every side's length should be shorter than sum of other two side
      if (((a + b) > c) && ((b + c) > a) && ((c + a) > b)){
    flag = 1;
    if ((a == b) && (b == c))
      printf ("\n It is an Equilatral Triangle");
    else if ((a == b) || (b == c) || (c == a))
      printf ("\n It is an isosceles Triangle");
    else
      printf ("\n It is a Scalene Triangle");
      }
    }
    
  if (flag == -1)
    printf ("Given side inputs, can't be a triangle ");
    
  getch ();
}

 

Testing Result of the Program

Not a Triangle

 Enter the values of a, b and c : = 11 5 5
Given side inputs, can't be a triangle 

Equilateral triangle

Enter the values of a, b and c : = 4 4 4

It is an Equilateral Triangle

Isosceles Triangle

Enter the values of a, b and c : = 2 3 2

It is an isosceles Triangle

 

Leave a Reply