ในปัญหานี้ เราได้รับฐาน (B) และสูง (H) ของสามเหลี่ยมมุมฉาก งานคือหาด้านตรงข้ามมุมฉาก
ทฤษฎีบทพีทาโกรัส
H² = B² + H² (ในที่นี้ H = hypotenuse, B = base, h = height)
Hypotenuse = √(base² + height²)
ตัวอย่าง
Input: B = 5, H = 12 Output: 13.00
√(5² + 12²) = √(25 + 144) = √169 = 13
ตัวอย่างโค้ด
#include <iostream>
#include <cmath>
using namespace std;
double findHypotenuse(double height, double base) {
return sqrt(height * height + base * base);
}
int main() {
double h = 5.0, b = 12.0;
cout << "Base: " << b << endl;
cout << "Height: " << h << endl;
cout << "Hypotenuse: " << findHypotenuse(h, b);
return 0;
}
ผลลัพธ์
Base: 12 Height: 5 Hypotenuse: 13