Determine the Type of Triangle Based on Side Lengths in C++

This C++ exercise focuses on conditional logic and input handling. The program asks the user to input the lengths of the three sides of a triangle. Based on these values, the program analyzes the relationship between the sides and determines whether the triangle is equilateral, isosceles, or scalene.

An **equilateral** triangle has all three sides equal, an **isosceles** triangle has two sides equal, and a **scalene** triangle has all three sides of different lengths. The logic implemented must consider all these conditions and output the correct triangle type accordingly.

This activity enhances your understanding of conditionals, logical operators, and basic geometry principles applied in programming.

Group

Advanced Conditional Structures in C++

Objective

1. Prompt the user to enter the lengths of three sides of a triangle.
2. Use conditional statements to compare the sides.
3. Print whether the triangle is equilateral, isosceles, or scalene.

Develop a program that determines the type of triangle (equilateral, isosceles, scalene).

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include input/output library

using namespace std;

int main() {
    // Declare variables to store the three side lengths
    float side1, side2, side3;

    // Ask the user to input the lengths of the three sides
    cout << "Enter the length of side 1: ";
    cin >> side1;

    cout << "Enter the length of side 2: ";
    cin >> side2;

    cout << "Enter the length of side 3: ";
    cin >> side3;

    // Check if the inputs can form a valid triangle using triangle inequality
    if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
        // If all three sides are equal, it is an equilateral triangle
        if (side1 == side2 && side2 == side3) {
            cout << "The triangle is equilateral." << endl;
        }
        // If two sides are equal, it is an isosceles triangle
        else if (side1 == side2 || side1 == side3 || side2 == side3) {
            cout << "The triangle is isosceles." << endl;
        }
        // If all sides are different, it is a scalene triangle
        else {
            cout << "The triangle is scalene." << endl;
        }
    } else {
        // If triangle inequality is not satisfied, it is not a valid triangle
        cout << "The entered lengths do not form a valid triangle." << endl;
    }

    return 0;  // End of program
}

 Output

Enter the length of side 1: 5
Enter the length of side 2: 5
Enter the length of side 3: 5
The triangle is equilateral.

Share this C++ Exercise


More C++ Programming Exercises of Advanced Conditional Structures in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.