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







<< PreviousNext >>

Simple Code for Listing Even Numbers in C++ - Fun Exercise for Young Students




What are Even Numbers? | Maths Explanation for C++ Kids

Hello pupils, what are even numbers?
Answer:
Even numbers are numbers that are divisible by 2.

They include:

2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, ...

Considering how simple and straight-forward even numbers are, writing a C++ code for even numbers serves as a great way to introduce our Mathematics educational activities for young learners.
Well then, let's see how we can write a programming code to make our computer list a set of even numbers in the C++ language, between a given range of values. Bear in mind, we use only a loop and a simple conditional statement for the C++ code.

Create a new C++ project; call it Arithmetic.
Create a new C++ class file; call it EvenNumbers.

Type out the adjoining C++ code for listing even numbers.


How to run C++ Codes

Run the code from the main function class by pressing key f6 or the run symbol.



So! C++ Fun Practice Exercise - List Even Numbers

As a fun practice exercise, feel free to try out your own boundary values, and see how the C++ code lists the even numbers between those boundary values.







C++ Code for Even Numbers - Header File.

#pragma once

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class EvenNumbers {
public:
    EvenNumbers(unsignedunsigned);
    virtual ~EvenNumbers();
    string prepResult();
private:
    unsigned int start; // Our starting point
    unsigned int stop; // where we will stop
    string result; // Store result here.
    stringstream aux; // helps us convert int to string
};


C++ Code for Even Numbers - Class File.

#include "stdafx.h"
#include "EvenNumbers.h"

/**
* Our constructor.
* @param first - for the start value
* @param last - for the end value
*/

EvenNumbers::EvenNumbers(unsigned firstunsigned last) {
    start = first;
    stop = last;
    aux << first;
    result = "Even numbers between " + aux.str();
    aux.str(""); // clear 'aux'
    aux << last;
    result += " and " + aux.str() + " are: \n";
    aux.str("");
}

/**
* nitty gritty
* @return - list of the required even numbers.
*/

string EvenNumbers::prepResult() {
    /*
    * Loop from start to stop and rip out odd numbers;
    */

    while (start <= stop) {
        if ((start % 2) == 0) { // modulo(%) is explained below
            aux << start;
            result = result + aux.str() + "; ";
            aux.str("");
        }
        start = start + 1; // increase start by 1
    }
    return result;
}

EvenNumbers::~EvenNumbers() {
}

C++ Code for Even Numbers - Main Class C++ code.

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

#include "stdafx.h"
#include "EvenNumbers.h"

#include <iostream>

using namespace std;


int main() {
    try {

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


        unsigned int start = 1, stop = 100;

        /*
        * Use the Even Number class.
        */

        EvenNumbers e_list(start, stop);
        cout << e_list.prepResult() << "\n";

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




<< PreviousNext >>