Rationalise (Canonise) the Fractions before Subtraction
Normally before fractions are subtracted, they are rationalised;
i.e., they are put in a form where their denominators become
the same. This same denominator is the LCM of the
denominators of all the separate fractions.
After this is done, the new numerators can then be subtracted.
Steps for Subtraction of Fractions VB.Net code
The following steps will guide us in writing our code.
Let's illustrate with
7/4 - 2/5
Step 1:
Find the LCM of the denominators.
⇒ LCM of 4 & 5 = 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.
⇒
((7 x 5) - (2 x 4))/20
= (35 - 8)/20
Step 3:
Go ahead and subtract the numerators.
⇒
27/20
Create a new class file; Project, Add Class.
Call it SubtractFraction.vb.
Optionally, create a new module file; Project, Add Module.
Call it SubtractFractionModule.vb.
Type out the adjoining Visual Basic (VB.Net) codes for subtracting 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!
Feel free to try out your own set of numerators and denominators for fractional subtraction.
VB.Net code for SubtractFraction Class File
Public Class SubtractFraction
Inherits AddFraction
Public Function doSubtract() As Dictionary(Of String, Integer)
canonizeFraction()
answer = new_numerators(0)
For count_off = 1 To new_numerators.Count - 1
answer -= new_numerators(count_off)
Next count_off
Dim send_back As New Dictionary(Of String, Integer)
send_back.Add("numerator", answer)
send_back.Add("denominator", lcm)
Return send_back
End Function
End Class
Main Module
Module Algebra_SubtractFraction
Sub Main()
Dim numerators = {9, 3, 5, 7}
Dim denominators = {2, 4, 12, 18}
Dim fractions As New Dictionary(Of String, Integer())
fractions.Add("numerators", numerators)
fractions.Add("denominators", denominators)
Console.WriteLine(" Solving:")
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("")
Dim sub_fract As New SubtractFraction
sub_fract._init_(fractions)
Dim fraction = sub_fract.doSubtract()
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