Triangle Valid or Not based On Angles: C Program


Three angles of a Triangle are entered through the keyboard, write a C program to check whether the Triangle is valid or not. The Triangle is valid if the sum of all the angles is exactly equal to 180.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program.

Logic To Find Valid Triangle or Not

We ask the user to enter all 3 angles of a Triangle. Then we add all these angles and if the result is 180 then its a valid Triangle, if not, its not a valid Triangle.

Formula To Calculate Valid Triangle
a + b + c = 180;

where a, b and c are 3 angles of a Triangle.

Note: Also not that if any of the angle is 0, then those 3 angles can’t form a Triangle, so it’s a invalid Triangle.

Triangle Valid or Not based On Angles: C Program


[youtube https://www.youtube.com/watch?v=io01X2kB66Q]

YouTube Link: https://www.youtube.com/watch?v=io01X2kB66Q [Watch the Video In Full Screen.]


Triangle Valid or Not based On Angles: C Program

#include < stdio.h >

int main()
{
    int a1, a2, a3, sum;

    printf("Enter 3 angles of a Triangle\n");
    scanf("%d%d%d", &a1, &a2, &a3);

    sum = a1 + a2 + a3;

    if(sum == 180 && a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("It's a valid Triangle\n");
    }
    else
    {
        printf("It's not a valid Triangle\n");
    }

    return 0;
}

Output 1:
Enter 3 angles of a Triangle
90
80
0
It’s not a valid Triangle

Output 2:
Enter 3 angles of a Triangle
90
90
90
It’s not a valid Triangle

Output 3:
Enter 3 angles of a Triangle
90
80
10
It’s a valid Triangle

Output 3 outputs valid Triangle as addition of all 3 angles gives 180.
90 + 80 + 10 = 180.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *