user13714373
user13714373

Reputation:

Program to define the type of a triangle

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.

Upvotes: 2

Views: 902

Answers (2)

AbdelAziz AbdelLatef
AbdelAziz AbdelLatef

Reputation: 3744

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;
}

Upvotes: 1

user8234870
user8234870

Reputation:

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

Upvotes: 1

Related Questions