Write a C++ Program to Check Triangle is Valid using Angles with an example. This C++ program allows users to enter three angles of a triangle and finds the sum of those. Next, we used the C++ If Else statement to check whether the sum is equal to 180. If True, it is a valid triangle; otherwise, it’s invalid.
#include<iostream> using namespace std; int main() { int angle1, angle2, angle3, sum; cout << "\nPlease Enter Three Angles = "; cin >> angle1 >> angle2 >> angle3; sum = angle1 + angle2 + angle3; if( sum == 180) { cout << "\nThis is a Valid Triangle"; } else cout << "\nThis is an Invalid"; return 0; }
Please Enter Three Angles = 30 60 90
This is a Valid Triangle
Let me check with wrong values.
Please Enter Three Angles = 30 90 120
This is an Invalid
C++ Program to Check Triangle is Valid using Angles Example 2
Apart from the sum of all them equal to 180, all the angles of the triangle should be non-zeros. In this C++ program, we check the same.
#include<iostream> using namespace std; int main() { int angle1, angle2, angle3, sum; cout << "\nPlease Enter Three Angles of a Triangle = "; cin >> angle1 >> angle2 >> angle3; sum = angle1 + angle2 + angle3; if(sum == 180 && angle1 != 0 && angle2 != 0 && angle3 != 0) { cout << "\nThis is a Valid Triangle"; } else cout << "\nThis is an Invalid Triangle"; return 0; }
