C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Decision Making Programs in C

Check whether triangle is equilateral, isosceles, or scalene

Introduction

A triangle can be classified into three types based on its sides:

  1. Equilateral Triangle → All three sides are equal.
    Example: a = 5, b = 5, c = 5
  2. Isosceles Triangle → Any two sides are equal.
    Example: a = 5, b = 5, c = 3
  3. Scalene Triangle → All sides are different.
    Example: a = 4, b = 5, c = 6

Before classification, we must ensure that the three sides form a valid triangle using the triangle inequality theorem:

  • a + b > c
  • a + c > b
  • b + c > a

Only if these conditions are satisfied can we classify the triangle.

 

C Program: Check Triangle Type

C

#include <stdio.h>

 

int main() {

    float a, b, c;

 

    // Input sides of triangle

    printf("Enter first side: ");

    scanf("%f", &a);

    printf("Enter second side: ");

    scanf("%f", &b);

    printf("Enter third side: ");

    scanf("%f", &c);

 

    // Check triangle validity

    if ((a + b > c) && (a + c > b) && (b + c > a)) {

 

        // Classify the triangle

        if (a == b && b == c) {

            printf("The triangle is Equilateral.\n");

        }

        else if (a == b || a == c || b == c) {

            printf("The triangle is Isosceles.\n");

        }

        else {

            printf("The triangle is Scalene.\n");

        }

 

    } else {

        printf("The given sides do not form a valid triangle.\n");

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter first side: 5
Enter second side: 5
Enter third side: 5
The triangle is Equilateral.

OUTPUT 2 :
Enter first side: 5
Enter second side: 5
Enter third side: 3
The triangle is Isosceles.

OUTPUT 3 :

Enter first side: 3
Enter second side: 4
Enter third side: 5
The triangle is Scalene.

OUTPUT 4 :

Enter first side: 1
Enter second side: 2
Enter third side: 10
The given sides do not form a valid triangle.
 

Explanation

  1. The program reads three side lengths: a, b, and c.
  2. It checks if the sides satisfy the triangle inequality theorem.
  3. If valid:
    • All sides equal → Equilateral.
    • Any two equal → Isosceles.
    • All different → Scalene.
  4. If not valid → It displays an error message.