How to calculate the length of legB and hypotenuse of a right-angle triangle with a given perimeter and legA in VB.NET

1 Answer

0 votes
Imports System

Module RightTriangle
    Sub Main()
        Dim perimeter As Double = 120
        Dim legA As Double = 20

        ' 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
        Dim legB As Double = (Math.Pow(perimeter - legA, 2) - Math.Pow(legA, 2)) /
                             (2 * (perimeter - legA))

        If legB <= 0 Then
            Console.Error.WriteLine("No valid right triangle exists with these values.")
            Return
        End If

        Dim hypotenuse As Double = Math.Sqrt(legA * legA + legB * legB)

        Console.WriteLine("Leg a: {0:F3}", legA)
        Console.WriteLine("Leg b: {0:F3}", legB)
        Console.WriteLine("Hypotenuse c: {0:F3}", hypotenuse)
    End Sub
End Module



' run:
'
' Leg a: 20.000
' Leg b: 48.000
' Hypotenuse c: 52.000
'

 



answered Nov 19 by avibootz

Related questions

...