Write a C program to check whether a triangle is equilateral, scalene or isosceles triangle using if else. Program to enter sides of a triangle and classify triangle as Equilateral, Scalene or Isosceles triangle using if else in C. 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 first side: 30 Input second side: 30 Input third side: 30
Output
Triangle is equilateral triangle
Required knowledge
Basic programming, If else, Basic Mathematics
Logic to check equilateral, scalene or isosceles triangle
Before we actually dive into the logic and programming section of this exercise. Let us first learn some basic properties of triangle and learn how to differentiate between triangles.
Properties of triangle
- A triangle is said Equilateral Triangle, if all its sides are equal. If a, b and c are three sides of triangle. Then a == b == c.
- A triangle is said Isosceles Triangle, if its two sides are equal. If a, b and c are three sides of triangle. Then either a == b or a == c or b == c.
- A triangle is said Scalene Triangle, if none of its sides are equal.
Below is the step by step descriptive logic to classify triangle as equilateral, scalene or isosceles triangle.
- Read three sides of a triangle and store it in some variable say a, b and c.
- Check if a == b and b == c. Then it is an equilateral triangle.
- If it is not an equilateral triangle. Then check if a == b or a == c or b == c. Then it is isosceles triangle.
- If it is neither equilateral nor isosceles triangle then it must be scalene triangle.
Let us implement the logic using C program
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 a, b, c; //a, b, c are three sides of a triangle /* Reads all sides of a triangle */ printf("Enter three sides of triangle: "); scanf("%d%d%d", &a, &b, &c); if(a==b && b==c) { //If all sides are equal printf("Equilateral triangle."); } else if(a==b || a==c || b==c) { //If two sides are equal printf("Isosceles triangle."); } else { //If none sides are equal printf("Scalene triangle."); } return 0; }
Enter three sides of triangle: 30 30 30 Equilateral triangle.
Happy coding ;)
You may also like
- 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.