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







<< Previous Next >>

How to Reduce Fractions to their Lowest Term in C++ | Kids Fun Project



What is Fraction Reduction to Lowest Term? | Maths Explanation for C++ Kids

Reducing fractions to their lowest terms is all about removing every common factor between the numerator and the denominator.

In this junior secondary C++ tutorial, you'll learn how to simplify fractions using a custom module. The C++ code provided helps reduce fractions to their lowest terms by finding common factors between the numerator and denominator. Whether you're a student or teacher, this math-focused C++ project makes fraction simplification easy and fun.



How the Reduce Fraction to Lowest Term Method Works | Detailed Explanation for C++ Kids

Let's see a C++ algorithm implementation to reduce a fraction to it's lowest term, using 12/16 as an example fraction.

A common factor to 12 and 16 is 2;
so removing 2 from 12/16 yields 6/8.

Again, 2 is common to both 6 and 8;
removing it yields 3/4.

We stop when nothing else is common (1 does not count).

Create a new C++ class file; call it LowestTerm.
Type out the adjoining C++ algorithm that reduces a fraction to its lowest term.



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 - Reduce Fraction to Lowest Term

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







C++ Code for Reducing Fractions to Lowest Term - Header File

#pragma once

class LowestTerm {
public:
    LowestTerm(unsignedunsigned);
    virtual ~LowestTerm();

    void reduceFraction();

    unsigned int numerator;
    unsigned int denominator;
private:
    unsigned int trial_factor;
};


C++ Code for Reducing Fractions to Lowest Term - Class File

#include "stdafx.h"
#include "LowestTerm.h"


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

    num < denom ? trial_factor = num : trial_factor = denom;
}

void LowestTerm::reduceFraction() {
    // We are counting down to test for mutual factors 
    while (trial_factor > 1) {
        // do we have a factor
        if ((numerator % trial_factor) == 0) {
            // is this factor mutual?
            if ((denominator % trial_factor) == 0) {
                // where we have a mutual factor
                numerator /= trial_factor;
                denominator /= trial_factor;
                continue// continue with next iteration, repeating the current value of trial_factor
            }
        }
        trial_factor--;
    }
}


LowestTerm::~LowestTerm() {
}

C++ Code for Reducing Fractions to Lowest Term - Main Class

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

#include "stdafx.h"
#include "LowestTerm.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 {
        /*
        * Reduce fractions to Lowest Term
        */

        numerator = 16;
        denominator = 640;

        cout << "\n    To reduce to lowest term, simplifying:\n";
        // Print as fraction
        printf("%46u\n", numerator);
        printf("%46s\n""-");
        printf("%46u\n", denominator);

        // use the LowestTerm class
        LowestTerm red_fract(numerator, denominator);
        red_fract.reduceFraction();

        printf("\n%46u\n", red_fract.numerator);
        printf("%46s\n""Answer =    -");
        printf("%46u\n", red_fract.denominator);

        cout << "\n\n";

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

    return 0;
}




<< Previous Next >>