C + + 기본 사항: IF-ELSE 문을 사용하여 삼각형 유형을 결정하는 방법

작성자

카테고리:

← 피드로
DEV Community · Alexandru · 2026-06-13 개발(SW)

Alexandru

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;
}

Enter fullscreen mode Exit fullscreen mode

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다