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

Previous Program Next Program

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

Logic to check triangle validity if angles are given

As I mentioned it above solving this program requires basic mathematics. You must be aware of basic properties of triangle before attempting this program. Let us first learn basic properties of triangle.

Property of a triangle

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

After knowing the basic property of triangle let us move on to the logic of this program. Below is the step by step descriptive logic to check whether a triangle can be formed or not, if angles are given.

  1. Read all three angles of the triangle in some variable say a, b and c.
  2. Find sum of all three angles, store the sum in some variable say sum = a + b + c.
  3. Check if sum == 180, then the triangle can be formed otherwise not.

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 a, b, c, sum; //a, b, c are three angles of a triangle

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


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


    /* Check the triangle validity property */
    if(sum == 180) 
    {
        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 ;)

You may also like

Previous Program Next Program
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
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment