The challenge for this month’s Exercise is to generate three random values and determine if they work as sides of a valid triangle. To make this determination, you must consider three types of triangles and test the generated values for each.
- An equilateral triangle has three sides of equal length.
- An isosceles triangle has two sides of equal length.
- A standard triangle has two sides of greater length than the third.
A right triangle fits into the last category, though this exercise doesn’t require you to test whether the triangle is right.
For my solution, the main() function generates the three random values: x, y, and z. Various functions are used to determine the triangle types:
The all_equal() function returns TRUE when all values are equal, indicating an equilateral triangle.
The two_equal() function returns TRUE when two of the values are equal, which represents an isosceles triangle.
The valid_triangle() function returns TRUE when the sum of two of the values is larger than the third. This function requires an additional function, longest(), which borrows the weirdo ternary solution from last month’s Exercise.
Here is the full code for my solution:
2026_01-Exercise.c
#include <stdio.h> #include <stdlib.h> #include <time.h> /* determine whether all sides are equal */ int all_equal(int a, int b, int c) { if( a==b==c ) return 1; return 0; } /* determine whether two sides are equal */ int two_equal(int a, int b, int c) { if( a==b || a==c || b==c ) return 1; return 0; } /* detect the longest side */ int longest(int a, int b, int c) { return( b>a ? b>c ? b : c : a>c ? a : c ); } /* validate three uneven sides as a triangle */ int valid_triangle(int a, int b, int c) { int h; h = longest(a,b,c); if( h==a && a < b+c ) return 1; if( h==b && b < a+c ) return 1; if( h==c && c < a+b ) return 1; return 0; } int main() { int x,y,z; /* seed the randomizer */ srand( (unsigned)time(NULL) ); /* generate three random values from 2 to 25 */ x = rand() % 24 + 2; y = rand() % 24 + 2; z = rand() % 24 + 2; /* validate the data for a triangle */ /* equilateral */ if( all_equal(x,y,z) ) printf("%d - %d - %d is an equilateral triangle\n",x,y,z); /* isosceles */ else if( two_equal(x,y,z) ) printf("%d - %d - %d is an isosceles triangle\n",x,y,z); /* standard */ else if( valid_triangle(x,y,z) ) { printf("%d - %d - %d is a valid triangle\n",x,y,z); printf("%d is the longest side\n",longest(x,y,z) ); } /* invalid */ else printf("%d - %d - %d is not a valid triangle\n",x,y,z); return 0; }
This code doesn’t require all the functions listed; only the longest() function is called twice. Even so, adding specific functions makes the main() function more readable. Here’s output from a few runs:
9 - 10 - 20 is not a valid triangle 19 - 16 - 4 is a valid triangle 19 is the longest side 16 - 16 - 12 is an isosceles triangle
To determine a right triangle you apply the Pythagorean theorem to any valid triangle. For example, if a is the longest side (hypotenuse) the following is true: if( a*a==b*b+c*c ) While this test isn’t that onery to code, the issue is determining which of the sides is the longest; the longest() function returns the longest side, but it doesn’t tell you which variable is longest: a, b, or c. Doing so just requires more code, which wasn’t part of the original challenge.
I hope that your solution met with success.