C program to input all sides of a triangle and check if triangle is valid or not

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

C program to determine the validity of a triangle based on its side lengths. Explore the triangle inequality theorem to efficiently check triangle validity.

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

Understanding Triangle Validity

For any triangle, the sum of the lengths of any two sides must be greater than the length of the third side. This principle, known as the triangle inequality theorem, forms the basis for determining the validity of a triangle based on its side lengths.

C program to input all sides of a triangle and check if triangle is valid or not

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

#include <stdio.h>

int main() {
    float side1, side2, side3;

    // Input from the user
    printf("Enter three sides of the triangle: ");
    scanf("%f %f %f", &side1, &side2, &side3);

    // Checking triangle validity
    if (side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) {
        printf("The triangle with side lengths %.2f, %.2f, and %.2f is valid.", side1, side2, side3);
    } else {
        printf("The given side lengths do not form a valid triangle.");
    }

    return 0;
}

Output

Enter three sides of the triangle: 12 10 11
The triangle with side lengths 12.00, 10.00, and 11.00 is valid.

Triangle Validity Check: The program applies the triangle inequality theorem to check if the sum of any two sides is greater than the length of the third side for all combinations.

In this article, we've created a simple C program that efficiently determines whether the given side lengths form a valid triangle. Applying the triangle inequality theorem through C programming allows us to validate triangles based on their side lengths.