Previous Page

Find the Greatest Divisor of two numbers - Visual Basic


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x, y, m, n, r As Integer
        getIntegers(m, n)
        x = m
        y = n
        Do While n <> 0
            r = n
            n = m Mod n
            m = r
        Loop
      
        TextBox1.Text = "The greatest common divisor of " & x & " and " & y & " is " & m & "."

    End Sub
    Sub getIntegers(ByRef m As Integer, ByRef n As Integer)

        Try
            m = CInt(InputBox("Enter the value of the first integer", "First Integer"))

            n = CInt(InputBox("Enter the value of the second integer", "Second Integer"))
        Catch
            MsgBox("Please make sure to enter only integers.", vbCritical, "Error!")

        End Try

    End Sub


End Class

Below is a sample input/output.

divisor input output
divisor input output
divisor input output
divisor input output