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







<< PreviousNext >>

Finding HCF and GCD in Visual Basic - Kids Fun Exercise



What is HCF and GCD? | Maths Explanation for VB.Net Kids

Highest Common Factor or Greatest Common Divisor is the highest-valued-factor or greatest-valued-divisor common to a set of numbers.

In this Visual Basic math project, we'll explore how to find the Highest Common Factor (HCF) and Greatest Common Divisor (GCD) of numbers. This tutorial is perfect for primary school students learning to code with Visual Basic.

A common method for finding H.C.F. - G.C.D. is repeated factorization using only common factors.

If we have the set of numbers 30, 48 and 54 for example, their H.C.F. or G.C.D. is found thus:

Steps for calculating HCF using factorization method in VB.Net
Figure: Math steps for calculating HCF using factorization method in VB.Net.

Hence, H.C.F. of 30, 48 and 54 = 2 X 3 = 6

How to find HCF of multiple numbers in Visual Basic | Step-by-step Guide.

We shall follow the steps below in our VB.Net algorithm for finding HCF.

Step 1:

Do a numerical sort on the set so its first member is the smallest in the set.

Step 2:

Starting with 2, iteratively check through the set of numbers for a common factor.

Step 3:

For each common factor, divide every member of the number set by the common factor.

Step 4:

Repeat from step 2 recursively until there are no more common factors.

Create a new VB.Net class file; Project, Add Class.
Call it findHCF.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it FindHCFModule.vb.
Type out the adjoining Visual Basic (VB.Net) code for finding Highest Common Factor (Greatest Common Divisor).(H.C.F)


Note: You can just reuse the VB.Net main module from the previous lesson if you have been following.


So! Visual Basic Fun Practice Exercise - Find HCF

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







VB.Net Code for Find HCF - Class File.

Public Class HCF

    Dim set_of_numbers As New List(Of Integer' will hold the the values to be sent in
    Dim common_factors As New List(Of Integer' for housing all common factors
    Dim all_round_factor As Boolean
    Dim calc_result As Integer
    Dim index As Integer

    ' A constructor
    Public Sub _init_(group As List(Of Integer))
        ' make copy Of argument
        For Each value In group
            set_of_numbers.Add(value)
        Next
        ' STEP 1:
        set_of_numbers.Sort() ' sort ascending

        all_round_factor = False ' boolean state flag
        calc_result = 1
    End Sub

    ' does the grunt work
    ' takes no arguments but requires 'set_of_numbers' to be set
    Private Function findHCFFactors() As List(Of Integer)
        ' use the smallest in the set for the range
        Do While index < set_of_numbers.Item(0)
            index += 1
            ' Check for factors common to every member of 'set_of_numbers'
            all_round_factor = True
            ' STEP 2:
            For count = 0 To set_of_numbers.Count - 1
                If all_round_factor And set_of_numbers.Item(count) Mod index <> 0 Then
                    all_round_factor = False
                End If
            Next count

            ' STEP 3:
            ' Divide every member Of 'set_of_numbers by each common factor
            If all_round_factor Then
                For count_off = 0 To set_of_numbers.Count - 1
                    set_of_numbers.Item(count_off) = CInt(set_of_numbers.Item(count_off) / index)
                Next count_off

                common_factors.Add(index)
                ' STEP 4:
                Return findHCFFactors()
            End If

        Loop
        Return Nothing

    End Function

    ' Returns a scalar value Of the HCF
    Public Function getHCF() As Integer
        index = 1
        findHCFFactors()

        'iterate through And retrieve members
        For Each factor In common_factors
            calc_result *= factor
        Next
        Return calc_result
    End Function

End Class

VB.Net Code for Find HCF - Main Module.

Module Arithmetic_HCF

    Sub Main()

        ' Use the HCF Class
        Dim group As New List(Of Integer)({20, 30, 40})
        Dim hcf As New HCF
        hcf._init_(group)
        Dim answer = hcf.getHCF()

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

    End Sub

End Module




<< PreviousNext >>