Find Missing Angle in a Triangle if two angles are given: C Program


We ask the user to input 2 angles of the Triangle. And using C Program we calculate and output the 3rd angle.

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

Formula To Calculate Valid Triangle

a + b + c = 180;

Formula To Find Missing Angle of Triangle

c = 180 – (a + b);

where a, b and c are angles of 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.

Find Missing Angle in a Triangle if two angles are given: C Program


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

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


Find Missing Angle in a Triangle if two angles are given: C Program

#include < stdio.h >

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

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

    a3 = 180 - ( a1 + a2 );

    if(a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("The 3rd angle is %d\n", a3);
    }
    else
    {
        printf("Invalid angles of Triangle\n");
    }

    return 0;
}

Output 1:
Enter 2 angles of Triangle
0
90
Invalid angles of Triangle

Output 2:
Enter 2 angles of Triangle
90
90
Invalid angles of Triangle

Output 3:
Enter 2 angles of Triangle
90
80
The 3rd angle is 10

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 *