C++ Program to Print Even Numbers

In this post, we will learn how to print even numbers using C++ Programming language.

Any number which is exactly divisible by 2 is called even numbers. For example: 2, 8, 32 and so on.

We will be computing the even numbers using the following methods:

  1. Using For Loop
  2. Using While Loop
  3. Even Numbers in a Range

So, without further ado, let’s begin this tutorial.

C++ Program to Print Even Numbers

// C++ Program to Print Even Numbers
#include <iostream>
using namespace std;

int main(){
    int num;
    
    // Asking for input
    cout << "Enter the maximum limit: " << endl;
    cin >> num;
    
    cout << "Even numbers from 1 to " << num << " are: " << endl;
    // Computing even numbers
    for (int i = 1; i <= num; i++){
        if (i % 2 == 0){
            cout << i << " ";
        }
    }
    return 0;
}

Output

Enter the maximum limit: 
19
Even numbers from 1 to 19 are: 
2 4 6 8 10 12 14 16 18 

How Does This Program Work ?

    int num;

In this program, we have declared an int data type variable num.

    // Asking for input
    cout << "Enter the maximum limit: " << endl;
    cin >> num;

Then, the user is asked to enter the maximum value up to which the user wants to print even numbers.

    for (int i = 1; i <= num; i++){
        if (i % 2 == 0){
            cout << i << " ";
        }
    }

Then, we used for loop to find and print all the even numbers lying between 1 and maximum value.

C++ Program to Print Even Numbers Using While Loop

// C++ Program to Print Even Numbers Using While Loop
#include <iostream>
using namespace std;

int main(){
    int num, i = 2;
    
    // Asking for input
    cout << "Enter the maximum limit; " << endl;
    cin >> num;
    
    cout << "Even numbers from 1 to " << num << " are: " << endl;
    // computing even numbers
    while (i <= num){
        cout << i << " ";
        i = i + 2;
    }
    return 0;
}

Output

Enter the maximum limit; 
15
Even numbers from 1 to 15 are: 
2 4 6 8 10 12 14 

C++ Program to Print Even Numbers in a Range

// C++ Program to Print Even Numbers in a Given Range
#include <iostream>
using namespace std;

int main(){
    int i, min, max;
    
    // Asking for input
    cout << "Enter the minimum limit: " << endl;
    cin >> min;
    cout << "Enter the maximum limit: " << endl;
    cin >> max;
    
    cout << "Even numbers from " << min << " to " << max << " are: " << endl;
    for (i = min; i <= max; i++){
        if (i % 2 == 0){
            cout << i << " ";
        }
    }
    return 0;
}

Output

Enter the minimum limit: 
5
Enter the maximum limit: 
25
Even numbers from 5 to 25 are: 
6 8 10 12 14 16 18 20 22 24 

Conclusion

I hope after going through this post, you understand how to print even numbers using C++ Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *