I am trying to check if 3 sides form a triangle in C++, but the answer for all possible numbers I tried it 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;
}
Required Knowledge. A triangle is a valid triangle, If and only If, the sum of any two sides of a triangle is greater than the third side. For Example, let A, B and C are three sides of a triangle. Then, A + B > C, B + C > A and C + A > B.
Algorithm. Step 1: Declare three sides of triangle. Step 2: Enter three sides at run time. Step 3: If side1 == side2 && side2 == side3 Go to step 6 Step 4: If side1 == side2 || side2 == side3 || side3 == side1 Go to Step 7 Step 5: Else Go to step 8 Step 6: Print the triangle is equilateral.
Let the sides be A, B, and C. The given triangle is right-angled if and only if A2 + B2 = C2.
Let's say that a, b, c is the sides of the triangle. Therefore, it must be satisfy this criteria :
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With