VOOZH about

URL: https://dev.to/alexandruv156/c-basics-how-to-determine-a-triangle-type-using-if-else-statements-ccf

⇱ C++ Basics: How to Determine a Triangle Type Using IF-ELSE Statements - DEV Community


A classic problem when learning C++ is checking what type of triangle you have based on 3 sides given by the user. Let's see how to write this logic cleanly using if-else structures without nesting too many conditions.



#include <iostream>
using namespace std;

int main() {
 double a, b, c;
 cout << "Enter three sides of the triangle: ";
 cin >> a >> b >> c;

 // Check if the sides can actually form a triangle
 if (a + b > c && a + c > b && b + c > a) {
 if (a == b && b == c) {
 cout << "The triangle is Equilateral" << '\n';
 }
 else if (a == b || b == c || a == c) {
 cout << "The triangle is Isosceles" << '\n';
 }
 else {
 cout << "The triangle is Scalene" << '\n';
 }
 } else {
 cout << "These sides cannot form a valid triangle" << '\n';
 }

 return 0;
}