C Program to Classify Triangle as Equilateral, Isosceles or Scalene

A triangle is classified by the relationship between its three sides:

  • Equilateral — all three sides are equal (all angles are 60°)
  • Isosceles — exactly two sides are equal (two base angles are equal)
  • Scalene — all three sides are different (all angles are different)

Before classifying, the program validates the triangle inequality: for any three lengths to form a triangle, the sum of any two sides must be strictly greater than the third. For example, sides 1, 2, 10 cannot form a triangle because 1+2=3 ≤ 10. The original post contained no code; this is a complete C89 implementation.

Sides Triangle inequality? Classification
3, 3, 3 3+3>3 ✓ Equilateral
3, 3, 5 3+3>5 ✓ Isosceles
3, 4, 5 3+4>5 ✓ Scalene
1, 2, 10 1+2≤10 ✗ Not a triangle

C Program: Classify a Triangle

/* Classify a triangle as equilateral, isosceles, or scalene
 * Compile: gcc -ansi -Wall -Wextra triangle.c -o triangle */
#include <stdio.h>

int main(void)
{
    int a, b, c;
    printf("Enter three sides of the triangle: ");
    if (scanf("%d %d %d", &a, &b, &c) != 3) {
        printf("Invalid input.\n");
        return 1;
    }
    if (a <= 0 || b <= 0 || c <= 0) {
        printf("Sides must be positive.\n");
        return 1;
    }
    if (a + b <= c || a + c <= b || b + c <= a) {
        printf("Not a valid triangle (triangle inequality violated).\n");
        return 1;
    }

    if (a == b && b == c)
        printf("Equilateral triangle (all three sides equal: %d, %d, %d).\n", a, b, c);
    else if (a == b || b == c || a == c)
        printf("Isosceles triangle (exactly two sides equal: %d, %d, %d).\n", a, b, c);
    else
        printf("Scalene triangle (all sides different: %d, %d, %d).\n", a, b, c);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra triangle.c -o triangle
./triangle

Sample Output

Enter three sides of the triangle: 3 3 3
Equilateral triangle (all three sides equal: 3, 3, 3).

Enter three sides of the triangle: 3 3 5
Isosceles triangle (exactly two sides equal: 3, 3, 5).

Enter three sides of the triangle: 3 4 5
Scalene triangle (all sides different: 3, 4, 5).

Enter three sides of the triangle: 1 2 10
Not a valid triangle (triangle inequality violated).

Code Explanation

  • Equilateral check firsta == b && b == c tests all three sides simultaneously. An equilateral triangle also satisfies the isosceles conditions (a == b, etc.) since any two sides are equal. Checking equilateral first and using else if for isosceles prevents an equilateral triangle from being misclassified as isosceles.
  • Isosceles: any two sides equal — the condition a==b || b==c || a==c covers all three possible pairs. If execution reaches this branch, we already know not all three are equal (equilateral was excluded), so matching any one pair confirms exactly two sides are equal.
  • Triangle inequality — three lengths form a triangle if and only if the sum of any two exceeds the third. All three conditions must be checked: a+b > c, a+c > b, b+c > a. Using <= (rather than <) correctly rejects degenerate triangles where two sides sum to exactly the third (which would be a straight line, not a triangle).
  • Integer sides — the program reads integers. For floating-point side lengths, use double with %lf and replace the equality checks with a tolerance: fabs(a-b) < 1e-9 instead of a == b, because floating-point values that are mathematically equal may differ by a tiny rounding error.

What This Program Teaches

  • Ordering if-else branches — equilateral is a special case of isosceles, so it must be checked first. This is the general principle: when conditions are nested subsets of each other, check the most specific first. Reversing the order would misclassify all equilateral triangles as isosceles.
  • Input validation before logic — checking the triangle inequality before the classification logic is cleaner and safer than letting the code classify “1, 2, 10” as scalene (which it would be, incorrectly, without the check).
  • Floating-point equality pitfall — comparing doubles with == fails for computed values due to rounding errors. Always use a tolerance for floating-point comparisons. Integer comparisons with == are exact and safe.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

1 comment on “C Program to Classify Triangle as Equilateral, Isosceles or Scalene

  • #include
    #include
    voidmain()
    {
    inta,b,c;
    lcrsc();
    printf("enter the values of a,b,c");
    scanf(:%d%d%d",&a,&b,&c);
    ifa+b>c&&b+c>a&&c+d>b)
    if(a==b&&b==c)
    {
    printf("equilateral triangle");
    }
    elseif(a==b||b==c)
    {
    printf("isosceles trianlge");
    }
    elseif(a!=b&&b!==c)
    {
    printf("scalar triangle");
    }
    else
    {
    printf("not a valid trianlge");
    }
    getch();
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>