How to check if three sides form a triangle in C ++

I am trying to check if the three sides form a triangle in C ++, but the answer to all the possible numbers that I tried says wrong ...

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, b, c;

    cin >> a >> b >> c;

    if (pow(a,2) == pow(b,2) * pow(c,2) || pow(b,2) == pow(a,2) * pow(c,2) || pow(c,2) == pow(a,2) * pow(b,2))
        cout << "The sides form a triangle" << endl;
    else
        cout << "The sides do not form a triangle." << endl;
    return 0;
}
+5
source share
6 answers

Say a, b, c are sides of a triangle. Therefore, it must satisfy these criteria:

  • a + b> c
  • a + c> b
  • b + c> a

All criteria must be true. If one of them is false, then a, b, c will not create a 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;
}
+16
source

Triangle conditions to check,

(a + b > c),
(b + c > a),
(c + a > b)
+3
source

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 || ...

1) sum of the squares of two sides equals the square of the longest side

:

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
+2

, , z ^ 2 = x ^ 2 + y + 2 :

 if (pow(a,2) == pow(b,2) * pow(c,2) || pow(b,2) == pow(a,2) * pow(c,2) || pow(c,2) == pow(a,2) * pow(b,2))

:

 if (pow(a,2) == pow(b,2) + pow(c,2) || pow(b,2) == pow(a,2) + pow(c,2) || pow(c,2) == pow(a,2) + pow(b,2))

- . 2 , , , .

, . , , .

+1

. , , , . n . . , b. b h = 3.

sort(b,b+h);
for (int j=0;j<(h-2);j++){
    if (b[j]+b[j+1]>b[j+2])
    {
return true;
    }
}
else {
return false;
}
0

, , : , , c, , , , a b. ,

if c < a+b {
   return true;
} else return false;

This is the essence of the triangle inequality theorem. Other conditions will be trivially true when it is a triangle, and it does not matter if this one condition is not. The key, of course, will sort the three sides to find the longest side using a simple sorting algorithm. The assumption in the code is that the sides are already sorted, so c is the longest.

0
source

All Articles