Click here to Skip to main content
15,881,753 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to write a program that for 3 input numbers it decides if they can form a triangle and if is rectangle or not . I wrote

#include <stdio.h>
int main(){
    int a , b, c;
    scanf("%d %d %d" , &a , &b , &c);
    if ((a==2) || (b==3) || (c==4))
    printf("The numbers can form a triangle.\n");
    
    else if ((a==3) || (b==4) || (c==5))
    printf("The numbers can form an rectangular triangle.\n");
    
    
    else if((a==1) || (b==1) || (c==5))
    printf("The numbers can form a triangle\n.");
    
    return 0;
}


The program is printing me the first 2 printf , but for the third print it prints "The numbers can form an rectangular triangle"

What I have tried:

i tried separate by semicolons but not working
Posted
Updated 19-Nov-22 3:48am

Assuming that a,b,c are the length of the sides, then you have to use the triangle inequality (see Triangle inequality - Wikipedia[^]) in order to estblish if they form a triangle.
Similarly, you have a right triangle if
z^2 = x^2 + y^2
holds, where
z = max{a,b,c}

and {x,y} are the remaining lengths.
 
Share this answer
 
Comments
merano99 19-Nov-22 8:13am    
+5
C++
if ((a==2) || (b==3) || (c==4))
printf("The numbers can form a triangle.\n");

In the above code if a equals 2 then you will print the message, regardless of the values of b and c. You need to test that all the values are equal, so it is a AND b AND c, thus:
C++
if ((a==2) && (b==3) && (c==4))
    printf("The numbers can form a triangle.\n");

You should als reduce larger values to see if they are in the same proportions as you are using here; for example [4, 6, 8], [12, 16, 20] etc.
 
Share this answer
 
You implement here among other things constants, which can result in a rectangular triangle (Pythagorean theorem). However, the question refers to an arbitrary triangle. It is not clear how a rectangle is to result from the specification of 3 lengths. Is it possible that a rectangular triangle is meant here instead of a rectangle? From two equal rectangular triangles one could also build a rectangle.

As Palini had already suggested, for any triangle the triangle inequality would be usable and to check if it is additionally right-angled the Pythagorean theorem would be useful afterwards.

According to the triangle inequality, in a triangle the length of the longest side c must always be shorter than or equal to the sum of the sides a and b. This means formally:

c <= a + b

In a rectangular triangle, the sum of the areas of the cathetus squares must be equal to the area of the hypotenuse square. Here, a and b are the lengths of the sides adjacent to the right angle, the cathetes, and c is the length of the side opposite the right angle, the hypotenuse. This means formally:

c^2 = a^2 + b^2

The program design presented above should better use these formulas than statically evaluate the lengths.
 
Share this answer
 
Quote:
The program is printing me the first 2 printf , but for the third print it prints "The numbers can form an rectangular triangle"

Because it is what( you requested.
Pay attention to the difference between '||' and "&&".
C++
if ((a==2) || (b==3) || (c==4))

With "||", test is true if 1 of the parts is true.
with "&&", test is true if all of the parts are true.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900