Home >>VB.Net Programs >VB.Net program to calculate the Highest Common Factor (HCF)

VB.Net program to calculate the Highest Common Factor (HCF)

Write a VB.Net program to calculate the Highest Common Factor (HCF)

Here, we can read the user's two integer numbers and calculate the Highest Common Factor and print it on the console screen.

Program :

The source code for the Highest Common Factor calculation is given below. The program given is compiled and successfully executed.

Example


	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

Explanation:

We created a Module1 module in the program above that contains the Main() method. Three local variables num1, num2, and temp have been generated in the Main() method that are initialized with 0. We then read the num1 and num2 value from the user after that.

While Not (num2 = 0)
    temp = num1 Mod num2
    num1 = num2
    num2 = temp
End While

In the code above, the Highest Common Factor was determined and the result was written on the console screen.


No Sidebar ads