Multiplying Fractions in Visual Basic: Master Junior Secondary Math through Coding
Learning how to multiply fractions in Visual Basic is a powerful way to bridge the gap between classroom mathematics and real-world computational thinking. In this tutorial, we move beyond basic arithmetic to build a Visual Basic algorithm for multiplying fractions that handles the heavy lifting for you — including reducing fractions to their lowest terms.
Whether you are a student looking for a Visual Basic math project or a teacher integrating STEM into your curriculum, this guide will show you how to code a fraction calculator from scratch.
Understanding the Math Behind the Visual Basic Code: How to Multiply Fractions
Before we dive into the Visual Basic code for fractions, let’s review the manual process. Multiplying fractions is straightforward: you multiply the numerators together and the denominators together. However, to get a professional result, we must simplify the product.
- Identify Common Factors: Find the Greatest Common Divisor (GCD) between any numerator and any denominator.
- Simplify (Cancellation): Divide out those common factors to keep the numbers manageable.
- Calculate the Product: Multiply the remaining numerators for your final numerator, and do the same for the denominators.
Example: '\( \frac{4}{9} \times \frac{21}{8} \)'
By identifying mutual factors (like 4 and 8, or 9 and 21), our Visual Basic logic can reduce this expression before performing the final multiplication.
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 \( \frac{4}{9} \times \frac{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 |
Visual Basic Program to Multiply Fractions
The same fraction multiplication method can be implemented using Visual Basic. This is useful for students learning how math concepts work in programming.
To the right is a simple Visual Basic fraction multiplication program that calculates the product of two fractions.
Create a new Visual Basic Class File;
Call it MultiplyFraction.vb.
Optionally, Create a new Visual Basic Module File;
Call it MultiplyFractionModule.vb.
Type out the adjoining Visual Basic (VB.Net) codes for multiplying fractions.
Why Learn Fraction Multiplication with Visual Basic?
Learning how to multiply fractions using Visual Basic helps students:
- Understand math concepts more clearly
- Improve logical thinking skills
- Connect mathematics with computer programming
- Practice writing simple math programs
This approach is especially useful for students learning both mathematics and Visual Basic.
Summary: Visual Basic Algorithm for Multiplying Fractions
By learning how to multiply fractions in Visual Basic, junior secondary students strengthen both their math and programming foundations. With step-by-step tutorials, simplification techniques, and coding exercises, mastering fraction multiplication becomes fun and practical.
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 these 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