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 all angles of the triangle 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.

Example:
Input first angle: 60
Input second angle: 30
Input third angle: 90
Output: Triangle is valid.

Required knowledge

Basic C programming, If else

Property of a triangle

We know that a triangle is valid if and only if sum of all three angles is 180°. Read more about properties of triangle.
Formula: a + b + c = 180° (Where a, b, c are three angles of a triangle).

Program to check valid triangle

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

#include <stdio.h>

int main()
{
    int a, b, c, sum; //a, b, c are three angles of a triangle

    /* Reads all three angles of triangle */
    printf("Enter three angles of triangle: \n");
    scanf("%d%d%d", &a, &b, &c);


    /* Calculate the sum of all angles of triangle */
    sum = a + b + c; 


    /* If sum=180 then its a valid triangle else not */
    if(sum == 180) 
    {
        printf("Triangle is valid.\n");
    }
    else
    {
        printf("Triangle is not valid.\n");
    }

    return 0;
} 


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

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment