Write a VB program to calculate GCD of two numbers.
Answers
First, this function determines the two numbers minimum. Then we check if both numbers are divided by the minimum. This is our GCD if it does, otherwise we will continue to check numbers below the minimum.
Module1
Sub Main()
{
Dim n1, n2 As Integer
Console.WriteLine("Enter first number")
n1 = Console.ReadLine
Console.WriteLine("Enter second number")
n2 = Console.ReadLine
Console.WriteLine("GCD of " & n1 & " and " & n2 & " is " & gcd(n1, n2))
Console.ReadLine()
}
Function gcd(ByVal n1 As Integer, ByVal n2 As Integer) As Integer
{
Dim minimum As Integer
{
If n1 < n2 Then
minimum = n1
{
Else
}
minimum = n2
}
For i As Integer = minimum To 1 Step -1
{
If n1 Mod i = 0 And n2 Mod i = 0 Then
{
Return i
}
}
}
}