Home »
VB.Net »
VB.Net Programs
VB.Net program to calculate the Highest Common Factor (HCF)
By Nidhi Last Updated : October 13, 2024
Calculating HCF in VB.Net
Here, we will read two integer numbers from the user and calculate the Highest Common Factor, and print that on the console screen.
VB.Net code to calculate the Highest Common Factor (HCF)
The source code to calculate the Highest Common Factor is given below. The given program is compiled and executed successfully.
'VB.Net program to calculate the Highest Common Factor
Module Module1
Sub Main()
Dim num1 As Integer = 0
Dim num2 As Integer = 0
Dim temp As Integer = 0
Console.Write("Enter number1: ")
num1 = Integer.Parse(Console.ReadLine())
Console.Write("Enter number2: ")
num2 = Integer.Parse(Console.ReadLine())
While Not (num2 = 0)
temp = num1 Mod num2
num1 = num2
num2 = temp
End While
Console.WriteLine("Highest Common Factor is: {0}", num1)
End Sub
End Module
Output:
Enter number1: 100
Enter number2: 40
Highest Common Factor is: 20
Press any key to continue . . .
Explanation:
In the above program, we created a module Module1 that contains the Main() method. In the Main() method we created three local variables num1, num2, and temp that are initialized with 0. After that, we read the value of num1 and num2 from the user.
While Not (num2 = 0)
temp = num1 Mod num2
num1 = num2
num2 = temp
End While
In the above code, we calculated the Highest Common Factor and print the result on the console screen.
VB.Net Basic Programs »