C Programming Code Examples C > If Else and Switch Case Code Examples Program to check valid triangle using nested if - best approach Program to check valid triangle using nested if - best approach Despite of easiness, the above code is messy and less readable. In above code printf("Triangle is not valid."); statement is unnecessarily repeated for various conditions. You can cut the extra printf("Triangle is not valid."); statement using a flag variable. Let us suppose a temporary variable valid initialized with 0 indicating triangle is not valid. The main idea is to check triangle validity conditions and if triangle is valid then set valid variable to 1 indicating triangle is valid. Finally, check if(valid == 1) then triangle is valid otherwise not valid. #include <stdio.h> int main() { int side1, side2, side3; /* Initially assume that the triangle is not valid */ int valid = 0; /* Input all three sides of a triangle */ printf("Enter three sides of triangle: \n"); scanf("%d%d%d", &side1, &side2, &side3); if((side1 + side2) > side3) { if((side2 + side3) > side1) { if((side1 + side3) > side2) { /* * If side1 + side2 > side3 and * side2 + side3 > side1 and * side1 + side3 > side2 then * the triangle is valid. Hence set * valid variable to 1. */ valid = 1; } } } /* Check valid flag variable */ if(valid == 1) { printf("Triangle is valid."); } else { printf("Triangle is not valid."); } printf("Happy Codings - C Programming Language Code Examples"); return 0; }