Welcome to our website! We are adding new data every day, so please check back often for updates.

# How to Write a C Program to Check if Three Values Can Form a Triangle

write a C program that reads three floating-point values and checks if is possible to make a triangle with them. determine the perimeter of triangle

In this program, you will learn how to write a C program that reads three floating-point values and checks if it is possible to make a triangle with them. You will also learn how to determine the perimeter of the triangle if the given values are valid.

#### What is a Triangle?

A triangle is a polygon with three sides and three angles. The sum of the angles of a triangle is always 180 degrees. The length of each side of a triangle can vary, but there is a condition that must be satisfied for any three values to form a valid triangle. This condition is called the triangle inequality theorem.

#### What is the Triangle Inequality Theorem?

The triangle inequality theorem states that for any three values a, b, and c, they can form a valid triangle only if the following inequalities hold:

• a + b > c
• b + c > a
• c + a > b

These inequalities mean that the sum of any two sides of a triangle must be greater than the third side. This ensures that the sides can meet at the vertices and form a closed shape. If any of these inequalities is violated, then the values cannot form a triangle.

For example, suppose we have three values 5, 7, and 10. We can check if they can form a triangle by applying the triangle inequality theorem:

• 5 + 7 > 10 (True)
• 7 + 10 > 5 (True)
• 10 + 5 > 7 (True)

Since all the inequalities are true, we can conclude that these values can form a valid triangle.

However, suppose we have three values 3, 4, and 9. We can check if they can form a triangle by applying the triangle inequality theorem:

• 3 + 4 > 9 (False)
• 4 + 9 > 3 (True)
• 9 + 3 > 4 (True)

Since one of the inequalities is false, we can conclude that these values cannot form a valid triangle.

#### C Program

Now that we understand the concept of triangles and the triangle inequality theorem, we can write the C program to implement it. The program will do the following steps:

• Declare three floating-point variables to store the input values
• Prompt the user to enter three values
• Read the input values and store them in the variables
• Check if the values are valid for a triangle using the triangle inequality theorem
• If valid, calculate the perimeter of the triangle and print it
• If not valid, print an error message

The code for the program is shown below:

#include <stdio.h>

int main()

{

float a, b, c;

printf("Enter three floating-point values:\n");

scanf("%f %f %f", &a, &b, &c);

if (a + b > c && b + c > a && c + a > b)

{

float perimeter = a + b + c;

printf("The perimeter of the triangle is %.2f\n", perimeter);

}

else

{

printf("The given values cannot form a triangle.\n");

}

return 0;

}

#### Output

To run and test the program, you need to compile it using a C compiler and execute it on your terminal or command prompt. You can use any online C compiler or IDE to run the program as well.

Here are some sample inputs and outputs of the program:

Enter three floating-point values:

5.6 7.8 9.2

The perimeter of the triangle is 22.60

Enter three floating-point values:

3.4 4.5 9.8

The given values cannot form a triangle.

Enter three floating-point values:

6.7 8.9 10.1

The perimeter of the triangle is 25.70

#### Explanation

The explanation of the above program is as follows:

1. The program starts by including the standard input/output header file 'stdio.h' which provides the functions 'printf' and 'scanf' for printing and reading data.
2. The program then defines the main function which is the entry point of the program. The main function returns an integer value which indicates the status of the program execution.
3. Inside the main function, the program declares three floating-point variables 'a', 'b', and 'c' to store the input values. Floating-point variables can store decimal numbers with fractional parts.
4. The program then prints a message to prompt the user to enter three values using the 'printf' function. The '\n' character is used to move to a new line after printing the message.
5. The program then reads the input values from the user using the 'scanf' function. The 'scanf' function takes a format string and a list of pointers to variables as arguments. The format string specifies the type and format of the input data. The '%f' specifier is used to indicate that the input data is a floating-point value. The '&' operator is used to get the address of the variables where the input data will be stored. The 'scanf' function will read three values from the user and store them in the variables 'a', 'b', and 'c'.
6. The program then checks if the values are valid for a triangle using the triangle inequality theorem. The triangle inequality theorem states that for any three values a, b, and c, they can form a valid triangle only if a + b > c, b + c > a, and c + a > b. The program uses the logical AND operator '&&' to combine these three inequalities into one condition. If this condition is true, then the program executes the statements inside the if block. Otherwise, it executes the statements inside the else block.
7. If the condition is true, then the program calculates the perimeter of the triangle by adding up the values of 'a', 'b', and 'c'. The program stores this value in another floating-point variable called 'perimeter'. The program then prints this value using the 'printf' function. The '%f' specifier is used to indicate that the value is a floating-point number. The '.2' modifier is used to limit the number of decimal places to two. The '\n' character is used to move to a new line after printing the value.
8. If the condition is false, then the program prints an error message using the 'printf' function. The error message informs the user that the given values cannot form a triangle.
9. Finally, the program returns 0 from the main function which indicates that it has executed successfully.

This is how the above program works and what it does.

#### Conclusion

In this program, you learned how to write a C program that reads three floating-point values and checks if it is possible to make a triangle with them. You also learned how to determine the perimeter of the triangle if the given values are valid. You also learned about the concept of triangles and the triangle inequality theorem. You can use this program to practice your C programming skills and learn more about geometry and logic. You can also modify the program to calculate other properties of triangles, such as area, angles, or types. Have fun coding!

We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!