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







<< Previous Next >>

How to Convert Improper Fraction to Mixed Fraction in C++ | Fun Maths Exercise



Understanding Fractional Numbers | Maths Explanation for C++ Kids

Welcome to this junior secondary C++ math project! In this tutorial, you'll learn how to convert improper fractions to mixed fractions using C++. This beginner-friendly guide is perfect for students exploring math with C++, and includes a simple C++ script to help you understand fraction conversion step by step.

Whether you're just starting out with C++ or looking for a fun math coding activity, this project will show you how to build a mixed fraction calculator using basic programming concepts.
Coding fractions in C++ is a fun way to combine math and programming. This project helps junior secondary students build confidence in both areas, and also teaches how to apply math logic in C++ code.

As before, fractional numbers can either be Proper (e.g. 2/5), Improper (e.g. 5/2), or Mixed (e.g. 21/2),


Step-by-Step Explanation of Improper Fraction to Mixed Fraction Conversion in C++

Say we have the fraction 10/3 - which is an improper fraction - we can write a C++ algorithm that converts this improper fraction to a mixed fraction by following a number of steps:

Step 1:

Find the largest number smaller than the numerator (10), that can divide the denominator - 3 - without remainder.
         ⇒ 9

Step 2:

Subtract the found number (9) - from Step 1 - from the numerator (10)
         ⇒ 10 - 9 = 1;
This yields the whole number.

Step 3:

Divide the found number (9) - from Step 1 - by the denominator (3)
         ⇒ 9/3 = 3;
This yields the new numerator.

Step 4:

Reuse the denominator (3) to get our mixed fraction
         ⇒ 31/3

Create a new C++ class file; call it ImproperToMixed
Type out the adjoining C++ code for converting improper to mixed fractions.


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


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

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







C++ Code for Converting Improper Fraction To Mixed Fraction - Header File

#pragma once

class ImproperToMixed {
public:
    ImproperToMixed(unsignedunsigned);
    virtual ~ImproperToMixed();
    void doConvert();

    unsigned int numerator;
    unsigned int denominator;
    unsigned int whole_number;
    unsigned int new_numerator;
};


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

#include "stdafx.h"
#include "ImproperToMixed.h"


ImproperToMixed::ImproperToMixed(unsigned numunsigned denom) {
    numerator = num;
    denominator = denom;
}

void ImproperToMixed::doConvert() {
    int dividend; // Highest multiple of denominator less than numerator
    // STEP 1:
    for (dividend = numerator - 1; dividend > 1; dividend--) {
        if ((dividend % denominator) == 0) {
            // STEP 2:
            new_numerator = numerator - dividend;
            // STEP 3:
            whole_number = dividend / denominator;
            break;
        }
    }
}


ImproperToMixed::~ImproperToMixed() {
}

C++ Code for Converting Improper Fraction To Mixed Fraction - Algebra Main class

// Algebra.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ImproperToMixed.h"

#include <iostream>
#include <exception>

using namespace std;

int main()
{
    cout << "\n    Welcome to our demonstration sequels";
    cout << "\n    Hope you enjoy (and follow) the lessons.\n\n";

    unsigned int numerator;
    unsigned int denominator;
    unsigned int whole_number;

    try {
        /*
        * Convert fractions from Improper to Mixed
        */

        numerator = 10;
        denominator = 3;

        cout << "\n    Converting from Improper to Mixed the fraction:\n";
        // Print as fraction
        printf("%56u\n", numerator);
        printf("%56s\n""-");
        printf("%56u\n", denominator);

        // use the ImproperToMixed class
        ImproperToMixed imp_mix(numerator, denominator);
        imp_mix.doConvert();

        printf("\n%52u\n", imp_mix.new_numerator);
        printf("%50s%u%s\n""Answer =   ", imp_mix.whole_number, "-");
        printf("%52u\n", denominator);

        cout << "\n\n";

    } catch (exception& e) {
        cout << "\n" << e.what() << "\n";
    }

    return 0;
}




<< Previous Next >>