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







<< Previous Next >>

How to Convert Mixed Fraction to Improper Fraction in C# | Junior Secondary Maths Activity



Understanding Fractions in Maths with C# Programming

Like we all know, fractional numbers can either be Proper, Improper , or Mixed .
A proper fraction has a denominator larger than the numerator, (e.g. 2/5).
An improper fraction has a numerator larger than the denominator, (e.g. 5/2).
A mixed fraction combines a whole number and a proper fraction, (e.g. 21/2).
Converting between these forms is a common task in algebra and C# math exercises.

In this tutorial, we'll walk through how to convert mixed fractions to improper fractions using C#. This is a great beginner C# math project for junior secondary students. By writing a simple C# class, you'll learn how to handle numeric data types and perform basic math operations with fractions.

Whether you're learning C# for school or just exploring math programming, understanding how to work with fractions in C# is a valuable skill. We'll use clear examples to show how to code fraction conversion and explain each step in detail.


Step-by-Step Guide to Converting Mixed Fractions to Improper Fractions in C#

Say we have the fraction 32/5 - which is a mixed fraction; we can implement a C# code that converts this mixed fraction to an improper fraction by following a number of steps:

Step 1:

Multiply the whole number (3), by the denominator (5)
         ⇒ 3 X 5 = 15

Step 2:

Add the result from Step 1 to the numerator (2)
         ⇒ 15 + 2 = 17

Step 3:

Our obtained improper fraction is our new numerator (17) divided by our denominator (5).

Create a new C# project; call it Algebra.
Create a new C# class file; call it MixedToImproper.

Type out the adjoining C# code for converting mixed to improper fraction.


So! C# Fun Practice Exercise - Convert Mixed Fraction to Improper Fraction

As a fun practice exercise, feel free to try out your own mixed fractions with different whole numbers, numerators and denominators, and see how the C# code simplifies those fractions.







C# Code for Converting Mixed Fraction To Improper Fraction - Class File

using System.Collections;

namespace Algebra
{
    class MixedToImproper
    {
        private int numerator;
        private int denominator;
        private int whole_number;

        public MixedToImproper(Hashtable fraction)
        {
            whole_number = (int)fraction["whole_number"];
            numerator = (int)fraction["numerator"];
            denominator = (int)fraction["denominator"];
        }

        public int doConvert()
        {
            // STEP 1, 2:
            return (whole_number * denominator) + numerator;
        }
    }
}

C# Code for Converting Mixed Fraction To Improper Fraction - Algebra Main Class

using System;
using System.Collections;

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

            /*
            * Convert fractions from Mixed to Improper
             */

            Hashtable fraction = new Hashtable();
            fraction["whole_number"] = 3;
            fraction["numerator"] = 1;
            fraction["denominator"] = 3;

            Console.WriteLine("    Converting from Mixed to Improper the fraction:");
            // Print as fraction
            Console.WriteLine(String.Format("{0,55}", fraction["numerator"]));
            Console.WriteLine(String.Format("{0,53} {1}", fraction["whole_number"], "-"));
            Console.WriteLine(String.Format("{0,55}", fraction["denominator"]));

            // use the MixedToImproper class
            MixedToImproper mix_imp = new MixedToImproper(fraction);
            fraction["numerator"] = mix_imp.doConvert();

            Console.WriteLine(Environment.NewLine);

            Console.WriteLine(String.Format("{0,52}", fraction["numerator"]));
            Console.WriteLine(String.Format("{0,52}""Answer =      -"));
            Console.WriteLine(String.Format("{0,52}", fraction["denominator"]));
        }
    }
}




<< Previous Next >>