Understanding the Math Behind Fraction Multiplication | Maths Explanation for Visual Basic Kids
Learning how to multiply fractions in Visual Basic is a great way to combine math skills with coding. This tutorial is designed for junior secondary students who want to understand fraction multiplication using simple Visual Basic classes and constructors.
Multiplying fractions is pretty straightforward:
Cancel out all common factors between numerators and denominators,
then multiply whatever is left numerator to numerator and
denominator to denominator.
In this lesson, we'll walk through the step-by-step method of multiplying fractions using Visual Basic. You'll learn how to define a class, use constructors, and apply logic to find mutual factors and simplify results.
Step-by-Step Explanation of Algorithm to Multiply Fractions in Visual Basic
This Visual Basic algorithm for fractions shows how to multiply two fractions and reduce them to their lowest terms.
It's a great math coding project for beginners.
Understanding how to multiply multiple fractions in Visual Basic helps students build both computational thinking and math fluency.
It's a foundational skill for more advanced topics like algebra and data science.
If we have
4/9 x 21/8;
Step 1:
Find any common factor between any numerator and any denominator.
Step 2:
Cancel out any such common factor.
| = | X | 7 | |
| 4 | 21 | ||
| 9 | 8 | ||
| 3 |
Step 3:
Repeat Steps 1 & 2 recursively until there are no more common factors.
| = | 1 | X | |
| 4 | 7 | ||
| 3 | 8 | ||
| 2 |
| = | 7 |
| 6 |
Create a new VB.Net class file; Project, Add Class.
Call it MultiplyFraction.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it MultiplyFractionModule.vb.
Type out the adjoining Visual Basic (VB.Net) codes for multiplying fractions.
Note: You can instead comment out the VB.Net code in the main module from the previous lesson or simply continue from where it stopped.
So! Visual Basic Fun Practice Exercise - Multiply 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 multiplies those fractions.
VB.Net Code for Multiplying Fractions - Class File
Protected numerators() As Integer
Protected denominators() As Integer
Protected trial_factor As Integer
Protected n_index As Integer
Protected d_index As Integer
Protected answer(2) As Integer
Protected mutual_factor As Boolean
' Simulate a constructor
Public Sub _init_(fractions As Dictionary(Of String, Integer()))
numerators = fractions.Item("numerators")
denominators = fractions.Item("denominators")
trial_factor = 0
n_index = 0
d_index = 0
answer = {1, 1}
' Set trial_factor To the highest value amongst
'both numerators And denominators
For Each numerator In numerators
If numerator > trial_factor Then
trial_factor = numerator
End If
Next
For Each denominator In denominators
If denominator > trial_factor Then
trial_factor = denominator
End If
Next
End Sub
' Returns a dictionary Of the New numerator And denominator
Public Function doMultiply() As Dictionary(Of String, Integer)
' STEP 3:
' We are counting down To test For mutual factors
Do While trial_factor > 1
' STEP 1:
' iterate through numerators And check For factors
Do While n_index < numerators.Count
mutual_factor = False
If numerators(n_index) Mod trial_factor = 0 Then ' do we have a factor
' iterate through denominators And check For factors
Do While d_index < denominators.Count
If denominators(d_index) Mod trial_factor = 0 Then ' Is this factor mutual?
mutual_factor = True
Exit Do ' stop as soon as we find a mutual factor so preserve the corresponding index
End If
d_index += 1
Loop
Exit Do ' stop as soon as we find a mutual factor so as to preserve the corresponding index
End If
n_index += 1
Loop
' STEP 2:
' where we have a mutual factor
If mutual_factor Then
numerators(n_index) = CInt(numerators(n_index) / trial_factor)
denominators(d_index) = CInt(denominators(d_index) / trial_factor)
Continue Do ' Continue With Next iteration repeating the current value Of trial_factor
End If
n_index = 0
d_index = 0
trial_factor -= 1
Loop
For Each numerator In numerators
answer(0) *= numerator
Next
For Each denominator In denominators
answer(1) *= denominator
Next
Dim send_back As New Dictionary(Of String, Integer)
send_back.Add("numerator", answer(0))
send_back.Add("denominator", answer(1))
Return send_back
End Function
End Class
VB.Net Code for Multiplying Fractions - Main Module
Sub Main()
''
' Multiplying fractions
''
Dim numerators = {16, 20, 27, 20}
Dim denominators = {9, 9, 640, 7}
Dim fractions As New Dictionary(Of String, Integer())
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}", "- X "))
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 MultiplyFraction Class
Dim mul_fract As New MultiplyFraction
mul_fract._init_(fractions)
Dim fraction = mul_fract.doMultiply()
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