পিথাগোরাস থিওরেম: সমকোণী ত্রিভুজে হাইপোটেনাস² = বেস² + হাইট²। C#-এ Math.Sqrt এবং Math.Pow ব্যবহার করে এটি ক্যালকুলেট করা যায়।
ফর্মুলা
double hypotenuse = Math.Sqrt(base * base + height * height);
// অথবা
double hypotenuse = Math.Sqrt(Math.Pow(base, 2) + Math.Pow(height, 2));
সম্পূর্ণ উদাহরণ
using System;
class PythagorasDemo
{
public static void Main()
{
double baseLen = 10;
double height = 5;
double hypotenuse = Math.Sqrt(baseLen * baseLen + height * height);
Console.WriteLine($"Base: {baseLen}");
Console.WriteLine($"Height: {height}");
Console.WriteLine($"Hypotenuse: {hypotenuse:F5}");
Console.WriteLine($"Hypotenuse (rounded): {Math.Round(hypotenuse, 2)}");
}
}
আউটপুট
Base: 10
Height: 5
Hypotenuse: 11.18034
Hypotenuse (rounded): 11.18
জেনেরিক মেথড
public static double CalculateHypotenuse(double a, double b)
{
return Math.Sqrt(a * a + b * b);
}
// ব্যবহার
double h = CalculateHypotenuse(3, 4); // 5
Math ক্লাসের অন্যান্য উপযোগী মেথড
| মেথড | বিবরণ |
|---|---|
Math.Sqrt(x) | স্কোয়ার রুট |
Math.Pow(x, y) | xʸ |
Math.Hypot(a, b) | √(a²+b²) — .NET Core 2.1+ / .NET 5+ (ওভারফ্লো-সেফ) |
Math.Atan2(y, x) | কোণ (রেডিয়ান) |
সেরা অনুশীলন
Math.Hypot ব্যবহার করুন (উপলব্ধ থাকলে) — বড় সংখ্যার জন্য ওভারফ্লো রোধ করে এবং আরও সঠিক।
// আধুনিক .NET-এ
double h = Math.Hypot(3, 4); // 5