triangle determine

can u help me this problem :

Input :
4 3 5
output:
right triangle

input :
4 4 5
output:
isoscles triangle

input:
4 4 4
output:
equilateral triangle

can u help me for this ????/


Hi,

An effort from your side would be lovely.... could you show us what you did/tried?what problems are you facing?
Let three variables a,b,c be the lengths. Take this value from user. Now use some if conditions to determine the result. Where exactly is the problem?
Submit what you already wrote inside the code tags.
Let's say the 3 sides are a, b, c.

Right triangle: Any of the 2 sides ([a,b], [b,c], [a,c]) squared and added equals the square of the 3rd.
Example: a^2 + b^2 = c^2 so sqrt(a^2 + b^2) = c.

Isosceles triangle: 2 of the sides are equal and one is different from the other 2.

Equilateral triangle: All of the sides are equal.

You should be able to check for these conditions easily enough.

[b]EDIT[/b]
Here's code if you don't want to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include <conio.h> // Optional. For getch(); which pauses program
int main()
{
    int a, b, c;
    std::cout << "Enter 3 sides of triangle\n";
    std::cout << "1st side: ";
    std::cin >> a;
    std::cout << "2nd side: ";
    std::cin >> b;
    std::cout << "3rd side: ";
    std::cin >> c;
    std::cout << '\n';
    if ((a == b) && (b == c) && (a == c))
    {
        std::cout << "This triangle is an equilateral triangle\n";
    }
    if (((a == b) && ((a != c) || (b != c))) || ((a == c) && ((a != b) || (c != b))))
    {
        std::cout << "This triangle is an isosceles triangle\n";
    }
    if (((std::sqrt (a * a + b * b)) == c) || ((sqrt (b * b + c * c)) == a) || ((sqrt (a * a + c * c)) == b))
    {
        std::cout << "This triangle is a right triangle\n";
    }
    getch(); // Optional. Pauses program.
    return 0;
}


Last edited on
@gentleguy

Mistake was thinking on the wrong train of thought. My code doesn't use arrays.
1
2
3
            A = acos((b*b+c*c-a*a)/(2*b*c));
            B = acos((a*a+c*c-b*b)/(2*a*c));
            C = acos((a*a+b*b-c*c)/(2*a*b));


Why on Earth does this result in a 270[°] triangle O_o ? (for 3,4,5)

Whole code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <math.h>

const double M_PI = 4*atan(1);

int main()
{
    char loop;

    do
        {
        int a, b, c; //assuming whole numbers
        double A, B, C; //angles
        std::cout << "Input triangle side lengths\n";
        std::cin >> a;
        std::cin >> b;
        std::cin >> c;

        if ((a==b) & (b==c))
        {
            std::cout << "Equilateral triangle.\nAngles are: A=B=A=60[°]";
        }
        else if (((a==b) & (a!=c)) | ((a==c) & (b!=c)))
        {
            A = acos((b*b+c*c-a*a)/(2*b*c));
            B = acos((a*a+c*c-b*b)/(2*a*c));
            C = acos((a*a+b*b-c*c)/(2*a*b));

            std::cout << "Isoscles triangle. Angles are: A=" << A << "[rad] B=" << B << "[rad] C="  << C << "[rad]\n";;
        }
        else if ((a*a==b*b+c*c) | (b*b==a*a+c*c) | (c*c==a*a+b*b))
        {

            A = acos((b*b+c*c-a*a)/(2*b*c));
            B = acos((a*a+c*c-b*b)/(2*a*c));
            C = acos((a*a+b*b-c*c)/(2*a*b));

            std::cout << "Right triangle. Angles are: A=" << A << "[rad] B=" << B << "[rad] C="  << C << "[rad]\n";
        }
        else
        {
            A = acos((b*b+c*c-a*a)/(2*b*c));
            B = acos((a*a+c*c-b*b)/(2*a*c));
            C = acos((a*a+b*b-c*c)/(2*a*b));

            std::cout << "Scalene triangle.Angles are: A=" << A << "[rad] B=" << B << "[rad] C="  << C << "[rad]\n";
        }

        std::cout << "\n\nEvaluate another triangle y/n?\n";
        std::cin >> loop;
        loop = toupper(loop);
        }
        while (loop == 'Y');

    return 0;

}
If inputs are int, then there is no need for floating-point math (e.g. acos and sqrt) and right angled isosceles triangle will not occur.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>

int main()
{
    double a = 0, b = 0, c = 0;     // sides
    double aa = 0, ab = 0, ac = 0;  // angles
    double p = 0;   // perimeter
    
    std::cout << "Please enter 3 sides: ";
    std::cin >> a >> b >> c;
    
    ac = acos( (a*a + b*b - c*c)/(2*a*b) );
    aa = acos( (b*b + c*c - a*a)/(2*a*b) );
    ab = M_1_PI - ac - aa;
    
    p = a + b + c;
    
    if( aa == M_PI/2 or ab == M_PI/2 or ac == M_PI/2)
        std::cout << "Right angled triangle\n";
    else if( p / 3 == a )
        std::cout << "Equilateral triange\n";
    else if ( p - a == 2*b or p - b == 2*a or p - c == 2*a)
        std::cout << "Isosceles triangle\n";
    else
        std::cout << "Scalene triangle\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void swap( int& a, int& b ) { const int temp = a ; a = b ; b = temp ; }

void make_ascending( int& a, int& b ) { if( b < a ) swap(a,b) ; }

void make_ascending( int& a, int& b, int& c ) { make_ascending(a,b) ; make_ascending(b,c) ; make_ascending(a,b) ; }

bool is_side( int a ) { return a > 0 ; }

bool is_triangle( int a, int b, int c ) // a, b, c in ascending order
{ return is_side(a) && is_side(b) && is_side(c) && (a+b) > c ;  }

enum type { isosceles, equilateral, right_angled, scalene_not_right_angled, not_a_triangle = -1 };

type triangle_type( int a, int b, int c )
{
    make_ascending(a,b,c) ;

    if( is_triangle(a,b,c) )
    {
        if( a==b ) return b==c ? equilateral : isosceles ;
        else return ( a*a + b*b == c*c ) ? right_angled : scalene_not_right_angled ;
    }
    else return not_a_triangle ;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    double a = 0, b = 0, c = 0;
    
    std::cout << "Please enter 3 sides: ";
    std::cin >> a >> b >> c;
    
    double a2 = a * a, b2 = b * b, c2 = c * c, p = a + b + c;
    
    if( a == b and b == c)
        std::cout << "Equilateral triange\n";
    else if ( p - a == 2 * b or p - b == 2 * a or p - c == 2 * a)
        std::cout << "Isosceles triangle\n";
    else if ( a2 == b2 + c2 or b2 == a2 + c2 or c2 == a2 + b2)
        std::cout << "Right angled triangle\n";
    else
        std::cout << "Scalene triangle\n";
}
Last edited on
Topic archived. No new replies allowed.