# C Program to Check Whether a Triangle is Valid or Not

Welcome to this informative blog post on writing a C program to check whether a triangle is valid or not. If you are new to programming or just starting your journey in the world of C programming, then this post is for you. We will cover all the necessary concepts and provide you with insights and tips to understand the logic behind solving this problem.

## Introduction

Triangles are an essential part of geometry and have various properties that make them interesting. One fundamental property of triangles is their validity. A triangle is considered valid if the sum of the lengths of any two sides is greater than the length of the remaining side.

In this blog post, we will explore how to write a C program to determine whether a triangle is valid or not based on its given sides. We will take you through the logic step by step, allowing you to understand the process thoroughly.

So let’s dive right in!

## Checking Triangle Validity

To check whether a triangle is valid or not, we need to compare the lengths of its three sides. Before proceeding with the programming part, let’s understand the logic behind it.

Consider a triangle with sides a, b, and c. To determine its validity, we need to verify the following conditions:

1. The sum of any two sides should be greater than the third side.

2. The sum of the lengths of any two sides should be greater than the length of the remaining side.

Using these conditions, we can write a C program that takes the lengths of three sides as input and checks whether a triangle with these sides is valid or not.

## Writing the C Program

Let’s start by creating a new C program and naming it `triangle_validity.c`. Open any text editor or integrated development environment (IDE) of your choice and follow along.

Before diving into the code, we need to calculate the sum of the lengths of any two sides. We can achieve this by using conditional statements in C programming.

``````#include <stdio.h>

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

printf("Enter the lengths of the three sides of a triangle: ");
scanf("%d %d %d", &side1, &side2, &side3);

// Check triangle validity conditions
if (side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) {
printf("Triangle is valid.\n");
} else {
printf("Triangle is not valid.\n");
}

return 0;
}``````

In the code snippet above, we have defined three variables `side1`, `side2`, and `side3` to store the lengths of the three sides of the triangle. We prompt the user to enter the lengths of the sides using the `printf` and `scanf` functions.

Next, we use conditional statements to check the validity of the triangle. If the given conditions are satisfied, we display the message “Triangle is valid.” Otherwise, we display “Triangle is not valid.”

## Running the Program

Now that we have written the C program, it’s time to compile and run it. If you are using an IDE, simply build and execute the program using the provided options. If you are using a command-line interface, follow the steps below:

1. Save the program with the `.c` extension and navigate to the directory where you saved it using the command-line interface.

2. Compile the program using the following command:

``gcc triangle_validity.c -o triangle_validity``

This will generate an executable file named `triangle_validity`.

3. Run the program by executing the following command:

``./triangle_validity``
4. Enter the lengths of the sides when prompted and press enter.

``Enter the lengths of the three sides of a triangle: 3 4 5``

In this example, we entered the lengths 3, 4, and 5 for the sides of the triangle.

5. The program will output whether the triangle is valid or not.

## Sample Output

Let’s provide a few more examples and their outputs to help you understand the program’s functionality better:

Example-1:

``````Enter the lengths of the three sides of a triangle: 5 12 13
Triangle is valid.``````

Example-2:

``````Enter the lengths of the three sides of a triangle: 2 3 6
Triangle is not valid.``````

Example-3:

``````Enter the lengths of the three sides of a triangle: 8 8 8
Triangle is valid.``````

## Conclusion

Congratulations! You have successfully written a C program to check whether a triangle is valid or not. Throughout this blog post, we discussed the logic behind checking triangle validity and provided a step-by-step guide to writing the program. We also provided examples with their outputs to help you understand the program’s functionality.

Remember, in a valid triangle, the sum of the lengths of any two sides is always greater than the length of the remaining side. By understanding and implementing this simple logic, you can easily check the validity of a triangle using a C program.

If you want to further explore the topic and enhance your programming skills, try modifying the program to calculate the type of triangle based on its side lengths or angles. This will allow you to delve deeper into the world of triangles and enhance your understanding of C programming concepts.

Keep exploring and practicing, and you will soon become proficient in writing C programs to solve a variety of problems!

Happy coding!