usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Visual Basic Program to Calculate HCF (GCD) with User Input - Kids Fun Project



Understanding the HCF Algorithm in Visual Basic

This Visual Basic program calculates the HCF (Highest Common Factor) using user input and the Fast HCF Visual Basic Code from the previous lesson.
Let's add some input mechanism so our user enters the set of numbers whose H.C.F. we are to find.


Combining the H.C.F. (G.C.D.) Visual Basic Code and Collecting User Input

All we need is a main code that asks the user for input.
For this purpose, we'll use the Console.ReadLine() library function.

How to use the HCF Code in Visual Basic

The user gets to enter his/her choice of numbers for the HCF VB.Net algorithm.
After entering each number, the user presses <Enter>.
After the user has entered every of his/her numbers, the user enters the word done to see the result.


Note: You can reuse the previous main module.



So! Visual Basic Fun Practice Exercise - Find HCF with User Input

As a fun practice exercise, feel free to try out your own numbers, and see how the Visual Basic code finds the HCF of your input numbers.







VB.Net Code for HCF with User Input - Main Module.

Imports System.Text.RegularExpressions

Module Arithmetic_HCFInput
    Sub Main()

        ' Collect input
        Console.WriteLine("Welcome to our Find HCF program.")
        Console.WriteLine("Enter your series of numbers whose HCF you wish to find.")
        Console.WriteLine("Type 'done' when you are through with entering your numbers.")

        Dim group As New List(Of Integer)
        Dim collect_input_text = "Enter First Number:  "
        Dim user_input As String
        Dim user_num As Integer

        Try
            Do
                ' Get keyboard input
                Console.Write(collect_input_text)
                user_input = Console.ReadLine()
                ' Make sure input is a number
                Dim check As New Regex("^[0-9]+$")
                Dim mate As Match = check.Match(user_input)
                If mate.Success Then
                    user_num = CInt(user_input)
                    If user_num <> 0 Then
                        group.Add(user_num)
                        collect_input_text = "Enter Next Number:  "
                    Else
                        Console.WriteLine("You cannot enter zero. Repeat that!Type 'done' when you're finished.")
                    End If

                Else
                    ' Convert 'user_input' to upper case.
                    user_input = user_input.ToUpper
                    If user_input <> "DONE" Then
                        Console.WriteLine("Wrong Input. Repeat that!Type 'done' when you're finished.")
                    End If
                End If
            Loop While user_input <> "DONE"
        Catch
            Console.WriteLine("Sorry, but something must have gone wrong!")
        End Try

        ' Use the fast HCF Class
        Dim h_c_f As New FastHCF
        h_c_f._init_(group)
        Dim answer = h_c_f.getHCF()

        Console.Write("The H.C.F. of " & String.Join(", ", group) & " is " & answer)

    End Sub

End Module



<< PreviousNext >>