usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

How to Add Fractions using C# | Step-by-Step Tutorial with Fun Exercises



Why Rationalise or Canonise Fractions before Addition | Maths Explanation for C# Kids

In this C# tutorial for junior secondary students, we explore how to add fractions. Before performing the addition, we rationalise or canonise the fractions to ensure accuracy. This method uses Finding LCM in C# class to align denominators, making it ideal for math programming beginners.
This C# tutorial teaches young students how to add fractions with different denominators.

Before fractions are added, they are rationalised; i.e., 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 added together.


Step-by-Step Guide for Addition of Fractions - C# Algorithm

The following steps will guide us in writing our C# code for adding fractions.
Let's illustrate the steps to follow with the example fractional expression 2/5 + 7/4

Step 1:

Using the Find LCM in C# class from the Primary Category, find the LCM of the denominators.
         ⇒ LCM of 5 & 4 = 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.
         ⇒ ((2 x 4) + (7 x 5))/20
         = (8 + 35)/20

Step 3:

Go ahead and add the numerators.
         ⇒ 43/20


Create a new C# class file; Call them AddFraction.;
Type out the adjoining C# code for adding fractions.


Note: The code module for Learn how to find LCM in C# is from the Primary Category.
Create a new C# class file called LCM in your current project and copy the L.C.M. code into it.

You can comment out the DivideFraction 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 adds these fractions.







C# Code for Adding Fractions - Class File

using System.Collections.Generic;

namespace Algebra
{
    class AddFraction
    {
        protected List<int> numerators = new List<int>();
        protected List<int> denominators = new List<int>();
        protected List<int> new_numerators = new List<int>();
        protected int lcm;
        protected int answer = 0;

        public AddFraction(List<int> num, List<int> denom)
        {
            numerators = num;
            denominators = denom;
        }

        protected void canonizeFraction()
        {
            // STEP 1:
            LCM l_c_m = new LCM(denominators);
            lcm = l_c_m.getLCM();

            // STEP 2:
            for (int i = 0; i < denominators.Count; i++)
            {
                new_numerators.Add(lcm / denominators[i] * numerators[i]);
            }
        }

        public int[] doAdd()
        {
            canonizeFraction();

            // STEP 3:
            foreach (int n in new_numerators)
            {
                answer += n;
            }
            return new int[] { answer, lcm };
        }
    }
}

C# Code for Adding 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");

            /*
            * Adding fractions
             */

            List<int> numerators = new List<int>();
            numerators.Add(1);
            numerators.Add(1);
            numerators.Add(1);
            numerators.Add(1);

            List<int> denominators = new List<int>();
            denominators.Add(4);
            denominators.Add(4);
            denominators.Add(4);
            denominators.Add(4);

            Console.WriteLine("    Solving:");
            // Print as fraction
            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();

            // use the AddFraction class
            AddFraction add_fract = new AddFraction(numerators, denominators);
            int[] solution = add_fract.doAdd();

            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]));

        }
    }
}



<< Previous Next >>