You are currently viewing C Program To Check Triangle Is Equilateral, Isosceles Or Scalene Triangle Using If-else

C Program To Check Triangle Is Equilateral, Isosceles Or Scalene Triangle Using If-else

In this tutorial, you will learn how to write a C program to check whether a triangle is equilateral, isosceles, or scalene triangle.

Problem Statement

We have to Write a C program that will check whether a triangle is equilateral, isosceles, or scalene based on the lengths of its sides. Our program will take the three sides of the triangle as input and print the output accordingly. if all sides are equal it will print triangle is an equilateral triangle. If the two sides are equal then the print triangle is an isosceles triangle. If no sides are equal then the print triangle is a scalene triangle.

Examples

Enter the lengths of the three sides of the triangle:
Side 1: 5
Side 2: 5
Side 3: 5
Output: The triangle is an equilateral triangle.

Enter the lengths of the three sides of the triangle:
Side 1: 5
Side 2: 5
Side 3: 6
Output: The triangle is an isosceles triangle.

Enter the lengths of the three sides of the triangle:
Side 1: 3
Side 2: 4
Side 3: 5
Output: The triangle is a scalene triangle.

Algorithm for Check Triangle Is Equilateral, Isosceles Or Scalene Triangle

  1. Start the program.
  2. Declare three variables side1, side2, and side3 of type float to store the lengths of the three sides of the triangle.
  3. Prompt the user to enter the lengths of the three sides of the triangle by printing the messages “Enter the lengths of the three sides of the triangle:” and “Side X:” (where X is 1, 2, or 3).
  4. Read the lengths of the three sides from the user and store them in the respective variables side1, side2, and side3 using scanf().
  5. Use if-else statements to determine the type of the triangle based on the lengths of its sides:
    • If side1, side2, and side3 are equal, print the message “The triangle is an equilateral triangle.”
    • Else if any two sides are equal (e.g., side1 == side2, side2 == side3, or side1 == side3), print the message “The triangle is an isosceles triangle.”
    • Else, if no sides are equal, print the message “The triangle is a scalene triangle.”
  6. End the program.

Program to check whether a triangle is equilateral, isosceles, or scalene triangle

#include <stdio.h>
int main() {
    float side1, side2, side3;
    printf("Enter the lengths of the three sides of the triangle:\n");
    printf("Side 1: ");
    scanf("%f", &side1);
    printf("Side 2: ");
    scanf("%f", &side2);
    printf("Side 3: ");
    scanf("%f", &side3);
    if (side1 == side2 && side2 == side3) {
        printf("The triangle is an equilateral triangle.\n");
    }
    else if (side1 == side2 || side2 == side3 || side1 == side3) {
        printf("The triangle is an isosceles triangle.\n");
    }
    else {
        printf("The triangle is a scalene triangle.\n");
    }
    return 0;
}

Output 1

Enter the lengths of the three sides of the triangle:
Side 1: 25
Side 2: 78
Side 3: 65
The triangle is a scalene triangle

Output 2

Enter the lengths of the three sides of the triangle:
Side 1: 43
Side 2: 43
Side 3: 12
The triangle is an isosceles triangle

Output 3

Enter the lengths of the three sides of the triangle:
Side 1: 55
Side 2: 55
Side 3: 55
The triangle is an equilateral triangle

Program Explanation

  • The program starts by including the necessary header file stdio.h.
  • The main() the function is defined.
  • Inside the main() function, three variables side1, side2, and side3 of type float are declared to store the lengths of the three sides of the triangle.
  • The program prompts the user to enter the lengths of the three sides of the triangle by printing the messages “Enter the lengths of the three sides of the triangle:” and “Side X:” (where X is 1, 2, or 3).
  • The scanf() the function is used to read the lengths of the three sides from the user, and the inputs are stored in the respective variables side1, side2, and side3 using %f format specifier.
  • If-else statements are used to determine the type of the triangle based on the lengths of its sides:
    • The condition side1 == side2 && side2 == side3 is checked to determine if all three sides are equal. If true, it prints the message “The triangle is an equilateral triangle.”
    • The conditions side1 == side2, side2 == side3, or side1 == side3 are checked to determine if any two sides are equal. If true, it prints the message “The triangle is an isosceles triangle.”
    • If none of the above conditions are true, it prints the message “The triangle is a scalene triangle.”
  • The return 0 statement ends the program.

Conclusion

This C program allows the user to enter the lengths of the three sides of a triangle and determines whether it is equilateral, isosceles, or scalene using if-else statements. It checks the conditions for each type of triangle and provides the appropriate output based on the lengths of the sides.