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

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

Write a C program to check whether a 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. 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, Relational operators, If else

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

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 triangle in some variable say angle1, angle2 and angle3.

2.Find sum of all three angles, store sum in some variable say sum = angle1 + angle2 + angle3.

3.Check if(sum == 180) then, triangle can be formed otherwise not. In addition, make sure angles are greater than 0 i.e. check condition for angles if(angle1 != 0 && angle2 != 0 && angle3 != 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; 

    /*
     * If sum of angles is 180 and
     * angle1, angle2, angle3 is not 0 then
     * triangle is valid.
     */
    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.




Instagram