Understanding the Math Behind Fraction Division | Maths Explanation for C# Kids
C# makes it easy to perform arithmetic operations like dividing fractions. Whether you're simplifying fractions, multiplying them,
or coding a math class project, C# provides clear syntax and powerful tools for beginners and students alike.
Dividing fractions in C# is a great way to combine coding with math skills. In this tutorial, junior secondary students
will learn how to use C# to divide fractions step-by-step. We'll explore how to invert and multiply fractions,
write a C# class for fraction operations, and understand the logic behind the algorithm.
Division is the inverse operation of multiplication; That is exactly what we'll do with for fractions.
Invert the fractions that come after a division sign, as well as change the division sign to multiplication, and then
follow through with multiplication.
Algorithm Steps to Divide Fractions in C#
Say we are to implement a C# algorithm to divide the given fractional expression
21/8 ÷ 7/2;
Inverting this will yield
21/8 ÷ 7/2;
Then we can go ahead and multiply.
Create a new C# class file;
Call it DivideFraction.
Type out the adjoining C# code for dividing fractions.
Note: You can comment out the MultiplyFraction C# object
code in the main class from the previous lesson or simply continue from where it stopped.
So! C# Fun Practice Exercise - Divide 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 Dividing Fractions - Class File
using System.Collections.Generic;
namespace Algebra
{
class DivideFraction : MultiplyFraction
{
public DivideFraction(List<int> num, List<int> denom) : base(num, denom)
{
}
public int[] doDivide()
{
int temp;
for (int i = 1; i < numerators.Count; i++)
{
temp = numerators[i];
numerators[i] = denominators[i];
denominators[i] = temp;
}
return doMultiply();
}
}
}
C# Code for Dividing 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(16);
numerators.Add(9);
numerators.Add(640);
numerators.Add(7);
List<int> denominators = new List<int>();
denominators.Add(9);
denominators.Add(20);
denominators.Add(27);
denominators.Add(20);
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();
DivideFraction div_fract = new DivideFraction(numerators, denominators);
int[] solution = div_fract.doDivide();
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]));
}
}
}