Skip to main content

Print Even and Odd Number in separate row/colum in one time - [C++ Solution]

Posted on:  at 
Problem Solving
Picture

Problem: Printing Even and Odd Numbers

Suppose you are a book writer and you need to quickly print even and odd numbers from 1 to a given value 'n' in separate rows or columns. Let's solve this problem using a simple C++ program.

Problem Solution

#include <iostream>
using namespace std;

int main()
{
    int num, a;
    num = 1;
    cout << "Enter how far you want to go: ";
    cin >> a;
    cout << "Odd numbers         " << "Even Numbers" << endl;
    while (num <= a) {
        if (num % 2 == 0) {
            cout << "                    " << num << " is Even" << endl;
        } else {
            cout << endl << num << " is Odd" << endl;
        }
        num = num + 1;
    }
    return 0;
}