Euclidisch algoritme


Sub CalculateGCD()
    ' Euclidean Algorithm with user input
    Dim a As Long, b As Long
    Dim temp As Long
    Dim originalA As Long, originalB As Long
    
    ' Get input from user
    a = InputBox("Enter first number:", "Euclidean Algorithm")
    b = InputBox("Enter second number:", "Euclidean Algorithm")
    
    ' Store original values for display
    originalA = a
    originalB = b
    
    ' Ensure positive numbers
    a = Abs(a)
    b = Abs(b)
    
    ' Handle zero cases
    If a = 0 And b = 0 Then
        MsgBox "GCD(" & originalA & ", " & originalB & ") = 0"
        Exit Sub
    End If
    
    If a = 0 Then
        MsgBox "GCD(" & originalA & ", " & originalB & ") = " & b
        Exit Sub
    End If
    
    If b = 0 Then
        MsgBox "GCD(" & originalA & ", " & originalB & ") = " & a
        Exit Sub
    End If
    
    ' Euclidean Algorithm
    Do While b <> 0
        temp = b
        b = a Mod b
        a = temp
    Loop
    
    ' Display result
    MsgBox "GCD(" & originalA & ", " & originalB & ") = " & a
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *