C Program to Check Whether a Triangle is Valid or Not given Angles of Triangle

  • Write a C program to read three angles of a triangle and check whether a triangle is valid or not.

Required Knowledge

  • C printf and scanf functions
  • If Else statement in C

A triangle is a valid triangle, If and only If the sum of all internal angles is equal to 180. This is known as angle sum property of triangle. We will use this triangle property to check whether three given angles can form a valid triangle.

C program to check whether a triangle is valid, given angles of triangle

C Program to Check Whether a Triangle is Valid or Not given Angles of Triangle

/*
 * Given three angles of a triangle, Write a c program to 
 * check whether it is a valid triangle or not 
 */ 
   
#include <stdio.h>  
   
int main() {  
    int angle1, angle2, angle3;  
   
    /* 
     * Take three angles of triangle as input 
     * from user using scanf 
     */ 
    printf("Enter Three Angles of a Triangle\n");  
    scanf("%d %d %d", &angle1, &angle2, &angle3);     
   
    /* 
     *Sum of all three angles of any triangle is 180 degree 
     */ 
    if( angle1 + angle2 + angle3 == 180) {  
        printf("It is a Valid Triangle\n");  
    } else {  
        printf("It is an invalid Triangle");  
    }  
   
    return 0;  
}

Output

Enter Three Angles of a Triangle
30 60 90
It is a Valid Triangle
Enter Three Angles of a Triangle
60 70 80
It is an invalid Triangle