Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

geometry

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;
}
like image 901
user2943407 Avatar asked Nov 07 '13 11:11

user2943407


People also ask

How do you check if three sides can form a triangle in C?

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.

Is triangle code in C?

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.

How do you check whether a triangle is right angled in C?

Let the sides be A, B, and C. The given triangle is right-angled if and only if A2 + B2 = C2.


1 Answers

Let's say that a, b, c is the sides of the triangle. Therefore, it must be satisfy this criteria :

  1. a + b > c
  2. a + c > b
  3. 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;
}
like image 55
david Avatar answered Nov 15 '22 06:11

david