Before going to the program to check triangle is Equilateral, Isosceles or Scalene first let us understand what is a Equilateral, Isosceles or Scalene Triangle?

Equilateral Triangle:

                  A Triangle is said to be an Equilateral Triangle if all the three sides are equal.

Isosceles Triangle:

                  A Triangle is said to be an Isosceles Triangle if its two side are equal.

Scalene Triangle:

                 A Triangle is said to be an Scalene Triangle if all the three sides are different.

Program code to check Equilateral, Isosceles or Scalene Triangle:

#include<stdio.h>
#include<conio.h> void main()
{ int a, b, c; clrscr(); printf("nn Enter three sides of triangle: "); scanf("%d %d %d", &a, &b, &c); if(a<=0 || b<=0 || c<=0) { printf("n INVALID INPUT"); } else if(a==b && b==c) { printf("n Equilateral Triangle"); } else if(a==b || a==c || b==c) { printf("n Isosceles Triangle"); } else { printf("n Scalene Triangle"); } getch();
}

Step by Step working of the above Program Code:

For Invalid Input:

  1. Let us assume that the user enters the three sides of triangle as 4 0 4.
  2. So it assigns the value of a=4, b=0, c=4.
  3. a<=0 or b<=0 or c<=0    (4<=0 or b<=0 or c<=0)   if condition is true
    • So it prints as INVALID INPUT.
  4. Thus the program execution is completed.

For Equilateral Triangle:

  1. Let us assume that the user enters the three sides of triangle as 10 10 10.
  2. So it assigns the value of a=10, b=10, c=10.
  3. a<=0 or b<=0 or c<=0    (10<=0 or 10<=0 or 10<=0)    if condition is false
    • So it goes to next else if part.
  4. a==b and b==c    (10==10 and 10==10)    if condition is true
    • So it prints as Equilateral Triangle.
  5. Thus the program execution is completed.

For Isosceles Triangle:

  1. Let us assume that the user enters the three sides of triangle as 30 20 30.
  2. So it assigns the value of a=30, b=20, c=30.
  3. a<=0 or b<=0 or c<=0    (30<=0 or 20<=0 or 30<=0)    if condition is false
    • So it goes to next else if part.
  4. a==b and b==c    (30==20 and 20==30)    if condition is false
    • So it goes to next else if part.
  5. a==b or a==c or b==c    (30==20 or 30==30 or 20==30)    if condition is true
    • So it prints as Isosceles Triangle.
  6. Thus the program execution is completed.

For Scalene Triangle:

  1. Let us assume that the user enters the three sides of triangle as 10 20 30.
  2. So it assigns the value of a=10, b=20, c=30.
  3. a<=0 or b<=0 or c<=0    (10<=0 or 20<=0 or 30<=0)    if condition is false
    • So it goes to next else if part.
  4. a==b and b==c    (10==20 and 20==30)    if condition is false
    • So it goes to next else if part.
  5. a==b or a==c or b==c    (10==20 or 10==30 or 20==30)    if condition is false
    • So finally it goes to else part and prints as Scalene Triangle.
  6. Thus the program execution is completed.

Output:

triangle1

triangle2

triangle3

triangle4

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

Similar Posts