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







<< PreviousNext >>

C++ Program to Calculate HCF (GCD) with User Input - Kids Fun Project



Understanding the HCF Algorithm in C++

This C++ program calculates the HCF (Highest Common Factor) using user input and the Fast HCF C++ Code from the previous lesson.
Let's add some input mechanism so our user enters the set of numbers whose H.C.F. we are to find.



Combining the H.C.F. (G.C.D.) C++ Code and Collecting User Input

All we need is a main class C++ code that asks the user for input.
For this purpose, we'll use the getline() and cin objects.

How to use the HCF Code in C++

The user gets to enter his/her choice of numbers for the HCF C++ algorithm.
After entering each number, the user presses <Enter>.
After the user has entered every of his/her numbers, the user enters the word done to see the result.


Note: You can comment out the C++ code for the main class from the previous lesson if you have been following.



So! C++ Fun Practice Exercise - Find HCF with User Input

As a fun practice exercise, feel free to try out your own numbers, and see how the C++ code finds the HCF of your input numbers.







C++ Code for HCF with User Input - Main Class.

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

#include "stdafx.h"
#include "FastHCF.h"

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <regex>

using namespace std;


int main() {
    try {

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


        vector<unsigned> set;

        // Collect input
        cout << "\nWelcome to our Find HCF program.\n";
        cout << "Enter your series of numbers whose HCF you wish to find.\n";
        cout << "\nType 'done' when you are through with entering your numbers.\n";
        cout << "Enter First Number:  ";

        set = {};
        string user_input;
        int user_num;
        regex verify_num("([0-9]+)"); //Regular expression object
        try {
            do {
                // get keyboard input
                getline(cin, user_input);
                // Convert 'user_input' to upper case.
                transform(user_input.begin(), user_input.end(), user_input.begin(), ::toupper);
                // Make sure input is a number
                if (regex_match(user_input, verify_num)) {
                    stringstream(user_input) >> user_num;
                    if (user_num != 0) {
                        set.push_back(user_num);
                        cout << "Enter Next Number:  ";
                    }
                    else {
                        cout << "\nYou cannot enter zero. Repeat that!\nType 'done' when you're finished.\n";
                    }
                }
                else if (user_input != "DONE") {
                    cout << "\nWrong Input. Repeat that!\nType 'done' when you're finished.\n";
                }
            } while (user_input != "DONE");
        }
        catch (exception& ex) {
            throw "Bad Input";
        }
        // use the fast class to create object
        FastHCF H_C_F(set);
        cout << "\n" << H_C_F.getHCF() << "\n";

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




<< PreviousNext >>