subtracting fractions with Different Denominators | Rationalising Fractions in C#
Learning how to subtract fractions in C# is an important step for junior secondary students who are beginning to
combine mathematics with coding. In this tutorial, we'll walk through the process of subtracting fractions with different denominators,
explain how to use the LCM (Lowest Common Multiple) method, and show you how to rationalise or canonise
fractions before subtraction. By the end, you'll be able to write a simple C# program to subtract fractions step by step,
giving you both a solid maths foundation and practical coding skills to implement libraries like the built-in C# fractions module.
Like with Adding Fractions in C#, fractional numbers are also rationalised before subtraction.
This means 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 subtracted.
Steps for Subtraction of Fractions with Different Denominators - C# Algorithm
The following steps will guide us in writing our C# code for subtracting fractions.
Let's illustrate these steps with the example fractional expression
7/4 - 2/5
Step 1:
Using the Find LCM in C#
class from the Primary Category, 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 C# class file;
call it SubtractFraction
Type out the adjoining C# code for subtracting fractions.
Note: You can comment out the AddFraction C# object
code in the main class from the previous lesson or simply continue from where it stopped.
So! C# 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 C# code divides those fractions.
C# Code for Subtracting Fractions - Class File
using System.Collections.Generic;
namespace Algebra
{
class SubtractFraction : AddFraction
{
public SubtractFraction(List<int> num, List<int> denom) : base(num, denom)
{
}
public int[] doSubtract()
{
canonizeFraction();
answer = new_numerators[0];
for (int i = 1; i < new_numerators.Count; i++)
{
answer -= new_numerators[i];
}
return new int[] { answer, lcm };
}
}
}
C# Code for Subtracting Fractions - Main Class
using System;
using System.Collections.Generic;
namespace Algebra
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to our demonstration sequels");
Console.WriteLine("Hope you enjoy (and follow) the lessons.");
Console.WriteLine("\r\n");
List<int> numerators = new List<int>();
numerators.Add(9);
numerators.Add(3);
numerators.Add(5);
numerators.Add(7);
List<int> denominators = new List<int>();
denominators.Add(2);
denominators.Add(4);
denominators.Add(12);
denominators.Add(18);
Console.WriteLine(" Solving:");
foreach (int n in numerators)
{
Console.Write(String.Format("{0,13}", n));
}
Console.Write(Environment.NewLine + String.Format("{0,12}", " "));
for (int i = 0; i < numerators.Count - 1; i++)
{
Console.Write(String.Format("{0}", "- - "));
}
Console.WriteLine(String.Format("{0,1}", "-"));
foreach (int d in denominators)
{
Console.Write(String.Format("{0,13}", d));
}
Console.WriteLine();
SubtractFraction sub_fract = new SubtractFraction(numerators, denominators);
int[] solution = sub_fract.doSubtract();
Console.WriteLine(Environment.NewLine);
Console.WriteLine(String.Format("{0,25}", solution[0]));
Console.WriteLine(String.Format("{0,25}", "Answer = -"));
Console.WriteLine(String.Format("{0,25}", solution[1]));
}
}
}