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







<< PreviousNext >>

How to Generate Odd Numbers in C++ for Kids




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

Odd numbers are numbers that are not divisible by 2.

They include:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

In this beginner-friendly Maths C++ tutorial for kids, we'll explore how to list odd numbers using C++ loops and conditions - perfect for primary school students learning to code.
To generate odd numbers in C++, we use a 'while' loop combined with a conditional statement. This simple C++ exercise will help you understand number patterns and logic.

Create a new C++ class file;
Call it OddNumbers.
Type out the adjoining C++ code for listing odd numbers.



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



Code for Odd Number List with User Input in C++

For a little more flexibility, let's add an input form to our C++ code for odd numbers.

All we need is a way to ask the user for input to limit the range of odd numbers.
For this purpose, we'll use the getline() and cin objects from the C++ libraries.



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

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







C++ Code for Odd Numbers - Header File.

#pragma once

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

using namespace std;

class OddNumbers {
public:
    OddNumbers(unsignedunsigned);
    virtual ~OddNumbers();
    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 Odd Numbers - Class File.

#include "stdafx.h"
#include "OddNumbers.h"

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

OddNumbers::OddNumbers(unsigned firstunsigned last) {
    start = first;
    stop = last;
    aux << first;
    result = "Odd 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 OddNumbers::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 += aux.str() + "; "// Mind the '+' in front of the '='
            aux.str("");
        }
        start++; // increase start by 1(same as start = start + 1
    }
    return result;
}

OddNumbers::~OddNumbers() {
}

C++ Code for Odd Numbers - Main class.

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

#include "stdafx.h"
#include "OddNumbers.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 odd number class.
        */

        OddNumbers o_list(start, stop); // using start and stop values as from before
        cout << "\n\n" << o_list.prepResult() << "\n";

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


C++ Code for Odd Numbers - Main class for Collecting Input.

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

#include "stdafx.h"
#include "OddNumbers.h"

#include <iostream>
#include <sstream>

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;

        /*
        * Collect Input
        */

        string user_str;
        cout << "\n\n" << "Enter the range for your odd numbers.\n";

        cout << "Enter Start Number: ";
        getline(cin, user_str); // Used for collecting user input.
        stringstream(user_str) >> start;

        cout << "Enter Stop Number: ";
        getline(cin, user_str);
        stringstream(user_str) >> stop;

        OddNumbers io_list(start, stop);
        cout << "\n" << io_list.prepResult() << "\n";

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




<< PreviousNext >>