#include <stdio.h>
#include <math.h>
int main(void) {
double perimeter = 120.0;
double legA = 20.0;
// From perimeter: a + b + c = P
// From Pythagoras: c = sqrt(a^2 + b^2)
// Substitute c into perimeter equation: a + b + sqrt(a^2 + b^2) = P
// Solve for legB
double legB = (pow(perimeter - legA, 2) - pow(legA, 2)) / (2.0 * (perimeter - legA));
if (legB <= 0) {
fprintf(stderr, "No valid right triangle exists with these values.\n");
return 1;
}
double hypotenuse = sqrt(legA * legA + legB * legB);
printf("Leg a: %.3f\n", legA);
printf("Leg b: %.3f\n", legB);
printf("Hypotenuse: %.3f\n", hypotenuse);
return 0;
}
/*
run:
Leg a: 20.000
Leg b: 48.000
Hypotenuse: 52.000
*/