Write a C program to input sides of a triangle and check whether a triangle is equilateral, scalene or isosceles triangle using if else. How to check whether a triangle is equilateral, scalene or isosceles triangle in C programming. Logic to classify triangles as equilateral, scalene or isosceles triangle if sides are given in C program.
Example
Input
Input
Input first side: 30 Input second side: 30 Input third side: 30
Output
Triangle is equilateral triangle
Required knowledge
Basic C programming, Relational operators, Logical operator, If else
Properties of triangle
- A triangle is said Equilateral Triangle, if all its sides are equal. If a, b, c are three sides of triangle. Then, the triangle is equilateral only if
a == b == c
. - A triangle is said Isosceles Triangle, if its two sides are equal. If a, b, c are three sides of triangle. Then, the triangle is isosceles if either
a == b
ora == c
orb == c
. - A triangle is said Scalene Triangle, if none of its sides are equal.
Logic to check equilateral, scalene or isosceles triangle
Step by step descriptive logic to classify triangle as equilateral, scalene or isosceles triangle.
- Input sides of a triangle from user. Store it in some variables say side1, side2 and side3.
- Check
if(side1 == side2 && side2 == side3)
, then the triangle is equilateral. - If it is not an equilateral triangle then it may be isosceles. Check
if(side1 == side2 || side1 == side3 || side2 == side3)
, then triangle is isosceles. - If it is neither equilateral nor isosceles then it scalene triangle.
Program to check equilateral, scalene or isosceles triangle
/**
* C program to check whether a triangle is Equilateral, Isosceles or Scalene
*/
#include <stdio.h>
int main()
{
int side1, side2, side3;
/* Input sides of a triangle */
printf("Enter three sides of triangle: ");
scanf("%d%d%d", &side1, &side2, &side3);
if(side1==side2 && side2==side3)
{
/* If all sides are equal */
printf("Equilateral triangle.");
}
else if(side1==side2 || side1==side3 || side2==side3)
{
/* If any two sides are equal */
printf("Isosceles triangle.");
}
else
{
/* If none sides are equal */
printf("Scalene triangle.");
}
return 0;
}
Output
Enter three sides of triangle: 30 30 30 Equilateral triangle.
Happy coding 😉
Recommended posts
- If else programming exercises index.
- C program to check whether a triangle is valid or not if all angles are given.
- C program to check whether a triangle is valid or not if sides are given.
- C program to find angle of a triangle if two angles are given.
- C program to find area of a triangle.
- C program to find the area of an Equilateral triangle.
- C program to find power of a number.