Even number

Even number are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

Logic

This program is similar to the previous one the only thing different here is, here we have to check if the number is completely divisible by 2 or not.

If the number is divisible by 2 then it is an even number, simply print it.

Program

#include <iostream>

using namespace std;

int main() {

    int i, n;

    // Take input from user.
    cout << "Print all even numbers till : ";
    cin >> n;

    cout << endl << "Even numbers from 1 to " << n << " are : " << endl;

    for(i = 1; i <= n; i++) {

        // Check for even or not.
        if((i % 2) == 0) {

            cout << i << " ";

        }
    }

    return 0;
}

Output

Print all even numbers till : 50
Even numbers from 1 to 50 are :
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50