Write a C program to enter three sides of a triangle and check whether the triangle is valid or not using if else. How to check whether a triangle can be formed or not if all sides of the triangle is given using if else in C programming. Program to check valid triangle if sides are known in C.

Input first side: 7

Input second side: 10

Input third side: 5

Output: Triangle is valid.

a + b > c and b + c > a and a + c > b

Read more about properties of triangle.

Happy coding ;)

**Example:**Input first side: 7

Input second side: 10

Input third side: 5

Output: Triangle is valid.

### Required knowledge

Basic C programming, If else### Logic to check triangle can be formed or not

We have already seen how to check valid triangle if angles are known. Here we will be checking if its sides are given. We know that a triangle is a valid triangle if and only if sum of its two sides is greater than the third side. Suppose a, b, c are three side of a triangle, then the triangle can only be formed ifa + b > c and b + c > a and a + c > b

Read more about properties of triangle.

### Program to check valid triangle

/** * C program to check whether a triangle is valid or not if its sides are given */ #include <stdio.h> int main() { int a, b, c; //a, b, c are three sides of a triangle /* Reads all three sides of a triangle */ printf("Enter three sides of triangle: \n"); scanf("%d%d%d", &a, &b, &c); if((a+b) > c ) { if((b+c) > a) { if((a+c) > b) { //If a+b>c and a+c>b and b+c>a then it is valid printf("Triangle is valid.\n"); } else { printf("Triangle is not valid.\n"); } } else { printf("Triangle is not valid.\n"); } } else { printf("Triangle is not valid.\n"); } return 0; }

**Note:**You can also short the above program using below approach.

### Program to check valid triangle

/** * C program to check whether a triangle is valid */ #include <stdio.h> int main() { int a, b, c; //a, b, c are three sides of a triangle int valid = 0; /* Reads all three sides of a triangle */ printf("Enter three sides of triangle: \n"); scanf("%d%d%d", &a, &b, &c); if((a+b)>c ) { if((b+c) > a) { if((a+c) > b) //If a+b>c and a+c>b and b+c>a then it is valid { valid = 1; } } } if(valid == 1) { printf("Triangle is valid.\n"); } else { printf("Triangle is not valid.\n"); } return 0; }

**Note:**Another way to short the program is by using logical AND && operator. Below program illustrates how to use logical AND operator to short the logic.

### Program to check valid triangle using logical AND operator

/** * C program to check whether a triangle is valid or not using logical AND operator */ #include <stdio.h> int main() { int a, b, c; //a, b, c are three sides of a triangle /* Reads all three sides of a triangle */ printf("Enter three sides of triangle: \n"); scanf("%d%d%d", &a, &b, &c); /* If a+b > c and a+c > b and b+c > a then triangle is valid */ if((a+b > c) && (a+c > b) && (b+c > a)) { printf("Triangle is valid.\n"); } else { printf("Triangle is not valid.\n"); } return 0; }

Output

Enter three sides of triangle: 7

4

10

Triangle is valid.

4

10

Triangle is valid.

Happy coding ;)

### You may also like

- If else programming exercises index.
- C program to check whether a triangle is Equilateral, Isosceles or Scalene triangle.
- C program to enter two angles of a triangle and find the third angle.
- C program to find the area of a triangle.
- C program to find area of an Equilateral triangle.
- C program to find maximum between three numbers.
- C program to enter any number and check whether it is negative, positive or zero.
- C program to check whether a number is even or odd.
- C program to enter P, T, R and calculate Simple Interest.
- C program to enter P, T, R and calculate Compound Interest.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).