C program to input angles of a triangle and check whether triangle is valid or not

Category: C Program
Tags: #cprogram#ifelse#conditional

C program to determine the validity of a triangle based on its angles. Explore the sum of interior angles to check triangle validity efficiently.

In the realm of geometry, validating a triangle's existence based on its angles is crucial. In this article, we'll explore how to create a simple C program that inputs angles of a triangle and determines whether the triangle is valid or not.

Understanding Triangle Validity

In a triangle, the sum of the interior angles is always 180 degrees. Therefore, if the sum of three given angles is equal to 180 degrees, the triangle is valid; otherwise, it's not a valid triangle.

C program to input angles of a triangle and check whether triangle is valid or not

Let's dive into the C programming language to create a program that validates a triangle based on its angles.

#include <stdio.h>

int main() {
    float angle1, angle2, angle3;

    // Input from the user
    printf("Enter three angles of the triangle: ");
    scanf("%f %f %f", &angle1, &angle2, &angle3);

    // Checking triangle validity
    float sum = angle1 + angle2 + angle3;

    if (sum == 180 && angle1 > 0 && angle2 > 0 && angle3 > 0) {
        printf("The triangle with angles %.2f, %.2f, and %.2f is valid.", angle1, angle2, angle3);
    } else {
        printf("The given angles do not form a valid triangle.");
    }

    return 0;
}

Output

Enter three angles of the triangle: 60 70 50
The triangle with angles 60.00, 70.00, and 50.00 is valid.

Triangle Validity Check: The program calculates the sum of the angles and checks if it equals 180 degrees. Additionally, it ensures that each angle is greater than 0 to validate a triangle.

In this article, we've created a simple C program that efficiently determines whether the given angles form a valid triangle. Understanding the property of the sum of interior angles in a triangle and applying it through C programming enables us to validate triangles based on their angles.


Recommended Posts