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







<< Previous Next >>

How to Add Fractions using Visual Basic | Step-by-Step Tutorial with Fun Exercises



Why Rationalise or Canonise Fractions before Addition | Maths Explanation for Visual Basic Kids

In this Visual Basic tutorial for junior secondary students, we explore how to add fractions. Before performing the addition, we rationalise or canonise the fractions to ensure accuracy. This method uses Finding LCM in Visual Basic class to align denominators, making it ideal for math programming beginners.
This Visual Basic tutorial teaches young students how to add fractions with different denominators.

Before fractions are added, they are rationalised; i.e., they are put in a form where their denominators become the same. This identical denominator is the LCM of the previous denominators of all the separate fractions.
After this is done, the new numerators can then be added together.


Step-by-Step Guide for Addition of Fractions - Visual Basic Algorithm

The following steps will guide us in writing our Visual Basic code for adding fractions.
Let's illustrate the steps to follow with the example fractional expression 2/5 + 7/4

Step 1:

Using the Find LCM in Visual Basic class from the Primary Category, find the LCM of the denominators.
         ⇒ LCM of 5 & 4 = 20

Step 2:

In a turn by turn fashion, divide the found LCM from Step 1 by each denominator, multiplying the quotient by the corresponding numerator.
         ⇒ ((2 x 4) + (7 x 5))/20
         = (8 + 35)/20

Step 3:

Go ahead and add the numerators.
         ⇒ 43/20


Create a new VB.Net class file; Project, Add Class.
Call it AddFraction.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it AddFractionModule.vb.
Type out the adjoining Visual Basic (VB.Net) codes for adding fractions.


Note: The code module for Learn how to find LCM in Visual Basic is from the Primary Category.
Create a new VB.Net class file called LCM in your current project and copy the L.C.M. code into it.

You can instead comment out the previous VB.Net code from the main module or simply continue from where it stopped.


So! Visual Basic Fun Practice Exercise - Add Fractions

As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the Visual Basic code adds these fractions.







VB.Net Code for Adding Fractions - Class File

Public Class AddFraction

    Protected numerators() As Integer
    Protected denominators() As Integer
    Protected new_numerators() As Integer

    Protected lcm As Integer
    Protected answer As Integer

    ' Simulate a constructor
    Public Sub _init_(fractions As Dictionary(Of StringInteger()))
        numerators = fractions.Item("numerators")
        denominators = fractions.Item("denominators")
        ReDim new_numerators(numerators.Count - 1)
        answer = 0
    End Sub

    ' transforms fractions so they all have same denominator
    ' takes a dictionary Of a list Of numerators And denominators
    '
    ' returns a dictionary Of the New numerators And New denominator(LCM)
    Protected Sub canonizeFraction()
        ' STEP 1:
        Dim l_c_m As New LCM
        l_c_m._init_(denominators)
        lcm = l_c_m.getLCM()

        ' STEP 2:
        ' make numerators vary(ratio) With lcm
        For index = 0 To denominators.Count - 1
            new_numerators(index) = CInt(lcm / denominators(index) * numerators(index))
        Next index

    End Sub

    ' returns a dictionary Of the
    ' New numerator And New denominator(LCM)
    Public Function doAdd() As Dictionary(Of StringInteger)
        canonizeFraction()

        ' STEP 3:
        ' add all transformed numerators
        For Each num In new_numerators
            answer += num
        Next

        Dim send_back As New Dictionary(Of StringInteger)
        send_back.Add("numerator", answer)
        send_back.Add("denominator"LCM)
        Return send_back

    End Function

End Class

VB.Net Code for Adding Fractions - Main Module

Module Algebra_AddFraction

    Sub Main()
        ''
        ' Adding fractions
        ''
        Dim numerators = {1, 1, 1, 1}
        Dim denominators = {4, 4, 4, 4}
        Dim fractions As New Dictionary(Of StringInteger())
        fractions.Add("numerators", numerators)
        fractions.Add("denominators", denominators)

        Console.WriteLine("    Solving:")
        ' Print as fraction
        For Each numerator In fractions.Item("numerators")
            Console.Write(String.Format("{0,13}", numerator))
        Next
        Console.Write(Environment.NewLine & String.Format("{0,12}"" "))
        For wasted = 0 To numerators.Count - 2
            Console.Write(String.Format("{0}""-     +      "))
        Next
        Console.WriteLine(String.Format("{0,1}""-"))
        For Each denominator In fractions.Item("denominators")
            Console.Write(String.Format("{0,13}", denominator))
        Next
        Console.WriteLine("")

        ' use the AddFraction Class
        Dim add_fract As New AddFraction
        add_fract._init_(fractions)
        Dim fraction = add_fract.doAdd()

        Console.WriteLine(Environment.NewLine)

        Console.WriteLine(String.Format("{0,25}", fraction.Item("numerator")))
        Console.WriteLine(String.Format("{0,25}""Answer   =   -"))
        Console.WriteLine(String.Format("{0,25}", fraction.Item("denominator")))

    End Sub

End Module



<< Previous Next >>