C program to check whether triangle is valid or not if angles are given

Write a C program to check whether triangle is valid or not if angles are given using if else. How to check whether a triangle can be formed or not, if its angles are given using if else in C programming. Program to check a valid triangle based on its angles in C. Logic to check triangle validity if angles are given in C program.

Example
Input

Input first angle: 60
Input second angle: 30
Input third angle: 90

Output

The triangle is valid

Required knowledge

Basic C programming, If else, Basic Mathematics

Property of a triangle

Validity of triangle with angles

A triangle is said to be a valid triangle if and only if sum of its angles is 180 °.

Logic to check triangle validity if angles are given

Below is the step by step descriptive logic to check whether a triangle can be formed or not, if angles are given.

  1. Input all three angles of the triangle in some variable say angle1, angle2 and angle3.
  2. Find sum of all three angles, store it in some variable say sum = angle1 + angle2 + angle3.
  3. If sum == 180 then, the triangle can be formed otherwise not. Make sure angles are greater than 0.

Program to check triangle validity when angles are given

/**
 * C program to check whether a triangle is valid or not if angles are given
 */

#include <stdio.h>

int main()
{
    int angle1, angle2, angle3, sum;

    /* Input all three angles of triangle */
    printf("Enter three angles of triangle: \n");
    scanf("%d%d%d", &angle1, &angle2, &angle3);


    /* Calculate sum of angles */
    sum = angle1 + angle2 + angle3; 


    /* Check the triangle validity property */
    if(sum == 180 && angle1!=0 && angle2!=0 && angle3!=0) 
    {
        printf("Triangle is valid.");
    }
    else
    {
        printf("Triangle is not valid.");
    }

    return 0;
}

Output

Enter three angles of triangle:
30
60
90
Triangle is valid.

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>