Write a C Program to Check Triangle is Valid or Not using Sides. Any triangle is valid if the sum of the two sides of a triangle is greater than the third side. For example, A, B, and C are sides of a triangle:
C Program to Check Triangle is Valid or Not using Sides Example 1
This program helps the user to enter all sides of a triangle. And then check whether it is valid or not using If Else.
/* C Program to Check Triangle is Valid or Not using Sides */ #include<stdio.h> int main() { int side1, side2, side3; printf("\n Please Enter Three Sides of a Triangle : "); scanf("%d%d%d", &side1, &side2, &side3); if( (side1 + side2 > side3) && (side2 + side3 > side1) && (side1 + side3 > side2) ) { printf("\n This is a Valid Tringle"); } else { printf("\n This is an Invalid Triangle"); } return 0; }
Let me try another value
Please Enter Three Sides of a Triangle : 20 30 90
This is an Invalid Triangle
C Program to Check Triangle is Valid or Not using Sides Example 2
In this program, we are using the C Programming Nested If Statement to check whether the triangle is valid or not.
/* C Program to Check Triangle is Valid or Not using Sides */ #include<stdio.h> int main() { int side1, side2, side3; printf("\n Please Enter Three Sides of a Triangle : "); scanf("%d%d%d", &side1, &side2, &side3); if(side1 + side2 > side3) { if(side2 + side3 > side1) { if(side1 + side3 > side2) { printf("\n This is a Valid Tringle"); } else { printf("\n This is an Invalid Triangle"); } } else { printf("\n This is an Invalid Triangle"); } } else { printf("\n This is an Invalid Triangle"); } return 0; }
Please Enter Three Sides of a Triangle : 25 65 56
This is a Valid Triangle
C Program to Check Triangle is Valid or Not using Sides Example 3
This program is the same as above, but this time, we removed the Else blocks. If the conditions inside the Nested If is true, then the flag value will be incremented to 1. Next, we are using the If Statement to check whether the flag value is on or not.
/* C Program to Check Triangle is Valid or Not using Sides */ #include<stdio.h> int main() { int side1, side2, side3; int flag = 0; printf("\n Please Enter Three Sides of a Triangle : "); scanf("%d%d%d", &side1, &side2, &side3); if(side1 + side2 > side3) { if(side2 + side3 > side1) { if(side1 + side3 > side2) { flag = 1; } } } if(flag == 1) { printf("\n This is a Valid Tringle"); } else { printf("\n This is an Invalid Triangle"); } return 0; }
Please Enter Three Sides of a Triangle : 7 5 6
This is a Valid Tringle