Description of valid triangles problem

valid-triangle-problem-solving-by-c-solution-of-valid-triangle

We know that the sum of three angle of any triangle is equal to 180. Here, we have to implement this logic to write the solution of valid triangles program. The program will take three angles from the user and check if the sum of all angles is 180 or not. If it is 180 then we have to print ‘YES’ otherwise ‘NO’.

This valid triangle program is not a hard problem at all. This problem is taken from CodeChef for practice and learning purpose only. Here in this C practice guide we will discuss the solution of this problem as well as the cause behind it.

Now, let’s go to the solution of this valid triangle program. We have used C language here although you can use any other language too. The logic may be same or different for you.

Solution of valid triangles problem using C

For solving this problem, at first we have to take an input from user which will be the test case for this problem. The each test case will take three integers.

After taking these input we will add the three angles and check if the sum is equal to 180 or not. When the sum is 180 then the triangle is possible otherwise not.

Suppose, a user gives three integers which is 60, 80 and 40.

Then the sum of these angle will be (60 + 80 + 40) = 180

So, this triangle is possible and it is a valid triangle.

But when a user gives three angles like 50, 30 and 80

Then the sum is = (50 + 30 + 80) = 160

As 160 is not equal to 180 then this triangle is not possible and is not a valid triangle.

// solution of valid triangle using C

#include <stdio.h>

int main(){
  int T, i;
  scanf("%d", &T);
  
  for(i = 1; i <= T; i++){
    int  a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    int sum = a + b + c;
    
    if(sum == 180){
        printf("YES\n");
    }else{
        printf("NO\n");
    }
    
  }
  return 0;
}

Output of valid triangles program

You will see the output like following when you compile and run  the above program.

3
40 40 100
YES
45 45 90
YES
180 1 1
NO

In the above output, first line which is 3, is the number of test case for this program. Then second line is the first input set for the program. Here the sum is equal to 180 and for this the program have printed ‘YES’. The other test case is also have worked like this.

However, you can implement other logic to solve this valid triangle program if you have any idea about solving this problem. The solution we have given here is not the only solution of this problem. Every problem have several solution and you have to discover your own.

Previous pageSecond largest number

Next page:  Lucky four