Understanding Fractional Numbers | Maths Explanation for Visual Basic Kids
Welcome to this junior secondary Visual Basic math project! In this tutorial, you'll learn how to
convert improper fractions to mixed fractions using Visual Basic. This beginner-friendly guide is perfect for students
exploring math with Visual Basic, and includes a simple Visual Basic script to help you understand fraction conversion step by step.
Whether you're just starting out with Visual Basic or looking for a fun math coding activity,
this project will show you how to build a mixed fraction calculator using basic programming concepts.
Coding fractions in Visual Basic is a fun way to combine math and programming.
This project helps junior secondary students build confidence in both areas, and also teaches how to apply math logic in Visual Basic code.
As before, fractional numbers can either be
Proper (e.g. 2/5),
Improper (e.g. 5/2), or
Mixed (e.g. 21/2),
Step-by-Step Explanation of Improper Fraction to Mixed Fraction Conversion in Visual Basic
Say we have the fraction 10/3 -
which is an improper fraction - we can write a Visual Basic algorithm that converts this improper fraction
to a mixed fraction by following a number of steps:
Step 1:
Find the largest number smaller than the numerator (10),
that can divide the denominator - 3 - without remainder.
⇒ 9
Step 2:
Subtract the found number (9) - from Step 1 -
from the numerator (10)
⇒ 10 - 9 = 1;
This yields the whole number.
Step 3:
Divide the found number (9) - from Step 1 -
by the denominator (3)
⇒ 9/3 = 3;
This yields the new numerator.
Step 4:
Reuse the denominator (3) to get our mixed fraction
⇒ 31/3
Create a new VB.Net class file; Project, Add Class.
Call it ImproperToMixed.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it ImproperToMixedModule.vb.
Type out the adjoining Visual Basic (VB.Net) code for converting improper to mixed 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 - Convert Improper Fraction to Mixed Fraction
As a fun practice exercise, feel free to try out your own improper fractions with different numerators and denominators,
and see how the Visual Basic code simplifies those fractions.
VB.Net Code for Converting Improper Fraction To Mixed Fraction - Class File
Public Class ImproperToMixed
Dim whole_number As Integer
Dim numerator As Integer
Dim denominator As Integer
Dim new_numerator As Integer
Public Sub _init_(fraction As Dictionary(Of String, Integer))
numerator = fraction.Item("numerator")
denominator = fraction.Item("denominator")
End Sub
Public Function doConvert() As Dictionary(Of String, Integer)
For dividend = numerator To 0 Step -1
If dividend Mod denominator = 0 Then
new_numerator = numerator - dividend
whole_number = CInt(dividend / denominator)
Exit For
End If
Next dividend
Dim send_back As New Dictionary(Of String, Integer)
send_back.Add("whole_number", whole_number)
send_back.Add("numerator", new_numerator)
send_back.Add("denominator", denominator)
Return send_back
End Function
End Class
VB.Net Code for Converting Improper Fraction To Mixed Fraction - Algebra Main Module
Module Algebra_ImproperToMixed
Sub Main()
Dim fraction As New Dictionary(Of String, Integer)
fraction.Add("numerator", 10)
fraction.Add("denominator", 3)
Console.WriteLine(" Converting from Improper to Mixed the fraction:")
Console.WriteLine(String.Format("{0,55}", fraction.Item("numerator")))
Console.WriteLine(String.Format("{0,55}", "-"))
Console.WriteLine(String.Format("{0,55}", fraction.Item("denominator")))
Dim imp2mix As New ImproperToMixed
imp2mix._init_(fraction)
fraction = imp2mix.doConvert()
Console.WriteLine(Environment.NewLine)
Console.WriteLine(String.Format("{0,52}", fraction.Item("numerator")))
Console.WriteLine(String.Format("{0,48} {1} {2}", "Answer = ", fraction.Item("whole_number"), "-"))
Console.WriteLine(String.Format("{0,52}", fraction.Item("denominator")))
End Sub
End Module