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







<< Previous Next >>

Multiplying Fractions with C#: A Step-by-Step Tutorial



Understanding the Math Behind Fraction Multiplication | Maths Explanation for C# Kids

Learning how to multiply fractions in C# is a great way to combine math skills with coding. This tutorial is designed for junior secondary students who want to understand fraction multiplication using simple C# classes and constructors.

Multiplying fractions is pretty straightforward:
Cancel out all common factors between numerators and denominators, then multiply whatever is left numerator to numerator and denominator to denominator.

In this lesson, we'll walk through the step-by-step method of multiplying fractions using C#. You'll learn how to define a class, use constructors, and apply logic to find mutual factors and simplify results.



Step-by-Step Explanation of Algorithm to Multiply Fractions in C#

This C# algorithm for fractions shows how to multiply two fractions and reduce them to their lowest terms. It's a great math coding project for beginners.
Understanding how to multiply multiple fractions in C# helps students build both computational thinking and math fluency. It's a foundational skill for more advanced topics like algebra and data science.

If we have
                  4/9 x 21/8;

Step 1:

Find any common factor between any numerator and any denominator.

Step 2:

Cancel out any such common factor.

= X7
421
98
3 
Step 3:

Repeat Steps 1 & 2 recursively until there are no more common factors.

=1X 
47
38
 2
=7
6

Create a new C# class file; call it MultiplyFraction. Type out the adjoining C# code for multiplying fractions.



Note: You can comment out the LowestTerm C# object code in the main class from the previous lesson or simply continue from where it stopped.


So! C# Fun Practice Exercise - Multiply 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 multiplies those fractions.







C# Code for Multiplying Fractions - Class File

using System.Collections.Generic;

namespace Algebra
{
    class MultiplyFraction
    {
        protected List<int> numerators = new List<int>();
        protected List<int> denominators = new List<int>();
        protected int[] answer = { 1, 1 };
        protected int n_index, d_index;
        protected int trial_factor;
        protected bool mutual_factor;

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

            trial_factor = 0;
            n_index = 0;
            d_index = 0;

            foreach (int n in numerators)
            {
                if (n > trial_factor)
                {
                    trial_factor = n;
                }
            }
            foreach (int d in denominators)
            {
                if (d > trial_factor)
                {
                    trial_factor = d;
                }
            }
        }


        public int[] doMultiply()
        {
            // STEP 3:
            // We are counting down to test for mutual factors 
            while (trial_factor > 1)
            {
                // STEP 1:
                // iterate through numerators and check for factors
                while (n_index < numerators.Count)
                {
                    mutual_factor = false;
                    if ((numerators[n_index] % trial_factor) == 0)
                    { // do we have a factor
                      // iterate through denominators and check for factors
                        while (d_index < denominators.Count)
                        {
                            if ((denominators[d_index] % trial_factor) == 0)
                            { // is this factor mutual?
                                mutual_factor = true;
                                break// stop as soon as we find a mutual factor so preserve the corresponding index
                            }
                            d_index++;
                        }
                        break// stop as soon as we find a mutual factor so as  preserve the corresponding index
                    }
                    n_index++;
                }
                // STEP 2:
                // where we have a mutual factor
                if (mutual_factor)
                {
                    numerators[n_index] = numerators[n_index] / trial_factor;
                    denominators[d_index] = denominators[d_index] / trial_factor;
                    continue; // continue with next iteration repeating the current value of trial_factor
                }
                n_index = 0;
                d_index = 0;
                trial_factor--;
            }

            for (int i = 0; i < numerators.Count; i++)
            {
                answer[0] *= numerators[i];
                answer[1] *= denominators[i];
            }

            return answer;
        }
    }
}

C# Code for Multiplying Fractions - Main Class

using System;
using System.Collections;
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");

            /*
            * Multiplying fractions
             */

            List<int> numerators = new List<int>();
            numerators.Add(16);
            numerators.Add(20);
            numerators.Add(27);
            numerators.Add(20);

            List<int> denominators = new List<int>();
            denominators.Add(9);
            denominators.Add(9);
            denominators.Add(640);
            denominators.Add(7);

            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}""-     X      "));
            }
            Console.WriteLine(String.Format("{0,1}""-"));
            foreach (int d in denominators)
            {
                Console.Write(String.Format("{0,13}", d));
            }
            Console.WriteLine();

            // use the MultiplyFraction class
            MultiplyFraction mul_fract = new MultiplyFraction(numerators, denominators);
            int[] solution = mul_fract.doMultiply();

            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 >>