How to check if 3 sides form a triangle in C++
Let's say that a, b, c is the sides of the triangle. Therefore, it must be satisfy this criteria :
- a + b > c
- a + c > b
- b + c > a
All the criteria must be true. If one of them are false, then a, b, c will not create the triangle.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
// check whether a, b, c can form a triangle
if (a+b > c && a+c > b && b+c > a)
cout << "The sides form a triangle" << endl;
else
cout << "The sides do not form a triangle." << endl;
return 0;
}
Triangle conditions to check for,
(a + b > c),
(b + c > a),
(c + a > b)
For a normal triangle
1. sum of any two sides is greater than third side (or)
2. difference of any two sides is less than third side
hint : a+b > c || ...
For a right angled triangle
1) sum of the squares of two sides equals the square of the longest side
Hint:
Find the longest side of three sides, that is find longest number in the three..
square the remaining two nums, add them and equate it to square of longest number