2

I think I am trapped with if/else if statements. Program to define what the type of the triangle is.

I don't understand, why it doesn't work properly. And, if it wouldn't be difficult, can you show how to optimize the work of right-angled triangle, using the Pythagorean theorem? In order to not mix the right-angled triangle with other triangles.

Code:

int main() {
    int a = 3;
    int b = 4;
    int c = 5;

    int angle_A = 100;
    int angle_B = 10;
    int angle_C = 70;

    if (a == b && a == c && c == b) {
        printf("Equilateral triangle\n");
    }
    else if (a == c || b == c || a == b) {
        printf("isosceles triangle.\n");
    }
    if ((pow(c, 2) == pow(b, 2) + pow(a, 2)) || (pow(a, 2) == pow(b, 2) || pow(c, 2)) || (pow(b, 2) == pow(c, 2) + pow(a, 2))) {
        printf("right-angled triangle.\n");
    }
    if ((angle_A < 90 || angle_B < 90 || angle_C < 90) && angle_A + angle_B + angle_C == 180) {
        printf("acute-angled triagle.\n");
    }
    if ((angle_A > 90 || angle_B > 90 || angle_C > 90) && angle_A + angle_B + angle_C == 180) {
        printf("An obtuse triangle.\n");
    }
return 0;
}

The output of this code:

right-angled triangle.
acute-angled triangle.
An obtuse triangle.
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
  • 2
    Short answer: comparing `double`s for equality isn't working out so well. Long answer: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Jabberwocky Oct 28 '20 at 12:32
  • BTW: what is the exepected output? – Jabberwocky Oct 28 '20 at 12:33
  • 2
    Note that the side lengths and angles of a triangle cannot be specified completely independently of each other. Note that the cases are not implemented to be mutually exclusive. – Codor Oct 28 '20 at 12:34
  • 3
    Aside: don't use `pow()` to square an `int`. – Weather Vane Oct 28 '20 at 12:34
  • Do you stand with "If one angle is less than 90, then it is acute-angled." and "If one angle is greater than 90, then it is obtuse angled." and "If the lenghts match Phytagoras then it is right angled." ? If yes, then your program is correct and you will have to look for the problem within those statements. Those statements can easily all be true after all (if you use lengths and angles which cannot match the same triangle). – Yunnosch Oct 28 '20 at 12:41
  • 1
    `(pow(a,2) == pow(b,2)||pow(c,2))` looks weird. Typo? – MikeCAT Oct 28 '20 at 12:44
  • @MikeCAT Oh, I missed that. I assume it a typo. – Yunnosch Oct 28 '20 at 12:45
  • Maybe `(pow(a,2) == pow(b,2)||pow(c,2))` -> `(pow(a,2) == pow(b,2) + pow(c,2)) `? Anyway the 3rd comment still applies. – Jabberwocky Oct 28 '20 at 12:46
  • The whole idea of your program is wrong. You should only need the side lengths a,b and c of the triangle and deduce the angles from a,b and c. Googling naively _Can you find the angles of a triangle given three sides?_ might help – Jabberwocky Oct 28 '20 at 12:50

2 Answers2

1
#include<stdio.h>
int main() {
    int a = 3;
    int b = 4;
    int c = 5;

    int angle_A = 100;
    int angle_B = 10;
    int angle_C = 70;

    if(angle_A+angle_B+angle_C==180){
       if (angle_A==60 && angle_B==60 && angle_C==60) {
            printf("Equilateral triangle\n");
        }
        else if (a==c || b==c || a==b) {
            printf("isosceles triangle.\n");
        }
        ///////If one of the angle is 90 then its a right angled triangle///////
        if (angle_A==90||angle_B==90||angle_C==90){
            printf("right-angled triangle.\n");
        }
        ////If all angles are less than 90 then its an acute angled triangle/////
        if (angle_A < 90 && angle_B < 90 && angle_C < 90) {
            printf("acute-angled triangle.\n");
        }
        ////If one of the angle is greater than 90 degree then its an obtuse angled triangle////
        if (angle_A > 90 || angle_B > 90 || angle_C > 90) {
            printf("An obtuse triangle.\n");
        }
    }
    return 0;
}

If an angle is 90 that's it. Then its a right angled triangle.

Udesh
  • 2,415
  • 2
  • 22
  • 32
  • Pythagorean theorem holds true for right angled triangle. So check is the angle is 90 degree. – Udesh Oct 28 '20 at 12:53
1

You have some mistakes, here are the details:

  1. The lengths should be double not int.
  2. You can't define both lengths and angles, as you can get the angles from the lengths.
  3. Get the longest length of the three, then check with Pythagorean theorem.
int main() {
    double t;
    double a = 3;
    double b = 4;
    double c = 5;

    if (a == b && a == c)
        printf("Equilateral triangle\n");

    else if (a == c || b == c || a == b)
        printf("isosceles triangle.\n");

    if (a < b)
    {
        t = a;
        a = b;
        b = t;
    }
    if (a < c)
    {
        t = a;
        a = c;
        c = t;
    }

    if (pow(a, 2) == pow(b, 2) + pow(c, 2))
        printf("Right-angled triangle.\n");
    else if (pow(a, 2) < pow(b, 2) + pow(c, 2))
        printf("Acute triagle.\n");
    else
        printf("Obtuse triangle.\n");
return 0;
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52