How to Print Even Numbers In C++?

9 minutes read

To print even numbers in C++, you can use a loop to iterate through a range of numbers and check if each number is even. Here is an example code that demonstrates how to achieve this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main() {
    int rangeStart, rangeEnd;
    
    // Asking for the range of numbers
    std::cout << "Enter the starting range number: ";
    std::cin >> rangeStart;
    
    std::cout << "Enter the ending range number: ";
    std::cin >> rangeEnd;
    
    // Printing even numbers in the given range
    std::cout << "Even numbers in the range " << rangeStart << " to " << rangeEnd << " are: \n";
    
    for (int i = rangeStart; i <= rangeEnd; i++) {
        if (i % 2 == 0) {
            std::cout << i << " ";
        }
    }
    
    return 0;
}


In this code, the program prompts the user to enter the starting and ending range numbers. It then iterates through each number in the given range and checks if it is divisible by 2 with no remainder using the modulus operator (%). If a number satisfies this condition, it is considered even and is printed to the console.

Best C++ Books to Read in 2024

1
C# & C++: 5 Books in 1 - The #1 Coding Course from Beginner to Advanced (2023) (Computer Programming)

Rating is 5 out of 5

C# & C++: 5 Books in 1 - The #1 Coding Course from Beginner to Advanced (2023) (Computer Programming)

2
C Programming Language, 2nd Edition

Rating is 4.9 out of 5

C Programming Language, 2nd Edition

3
Effective C: An Introduction to Professional C Programming

Rating is 4.8 out of 5

Effective C: An Introduction to Professional C Programming

4
The C Programming Language

Rating is 4.6 out of 5

The C Programming Language

5
C Programming: A Modern Approach, 2nd Edition

Rating is 4.5 out of 5

C Programming: A Modern Approach, 2nd Edition

6
C Programming Absolute Beginner's Guide

Rating is 4.4 out of 5

C Programming Absolute Beginner's Guide

7
Learn C Programming: A beginner's guide to learning the most powerful and general-purpose programming language with ease, 2nd Edition

Rating is 4.3 out of 5

Learn C Programming: A beginner's guide to learning the most powerful and general-purpose programming language with ease, 2nd Edition

8
C Programming For Dummies (For Dummies (Computer/Tech))

Rating is 4.2 out of 5

C Programming For Dummies (For Dummies (Computer/Tech))


What is the syntax for printing numbers in C++?

To print numbers in C++, you can use the cout object from the iostream library. Here's an example of the syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <iostream>

int main() {
    int num = 42;
    std::cout << num << std::endl;

    double pi = 3.14159;
    std::cout << pi << std::endl;

    return 0;
}


In this example, std::cout is used to output the values of num and pi. The std::endl is used to insert a new line after each number.


What is the modulus operator in C++ and how to use it to print even numbers?

The modulus operator in C++ is represented by the symbol %. It calculates the remainder of a division operation.


To print even numbers using the modulus operator in C++, you can divide a number by 2 and check if the remainder is 0. If the remainder is indeed 0, then the number is even and you can print it.


Here's an example code to print even numbers between 1 and 10 using the modulus operator:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <iostream>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            std::cout << i << " ";
        }
    }
    return 0;
}


This code uses a for loop to iterate from 1 to 10. For each number, it checks if the remainder of dividing it by 2 is 0. If it is, the number is even and it gets printed to the console. In this case, the output will be: 2 4 6 8 10.


How to print even numbers using different programming paradigms in C++?

There are multiple programming paradigms in C++, such as procedural, object-oriented, functional, and generic. Here's how you can print even numbers using each of these paradigms:

  1. Procedural paradigm:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>

void printEvenNumbers(int start, int end) {
    for (int i = start; i <= end; i++) {
        if (i % 2 == 0) {
            std::cout << i << " ";
        }
    }
}

int main() {
    int start = 1, end = 10;
    printEvenNumbers(start, end);

    return 0;
}


  1. Object-oriented paradigm:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

class EvenNumberPrinter {
public:
    void printEvenNumbers(int start, int end) {
        for (int i = start; i <= end; i++) {
            if (i % 2 == 0) {
                std::cout << i << " ";
            }
        }
    }
};

int main() {
    EvenNumberPrinter printer;
    int start = 1, end = 10;
    printer.printEvenNumbers(start, end);

    return 0;
}


  1. Functional paradigm (using recursion):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>

void printEvenNumbers(int start, int end) {
    if (start > end) {
        return;
    }
    if (start % 2 == 0) {
        std::cout << start << " ";
    }
    printEvenNumbers(start + 1, end);
}

int main() {
    int start = 1, end = 10;
    printEvenNumbers(start, end);

    return 0;
}


  1. Generic paradigm:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <algorithm>

template <typename Iterator>
void printEvenNumbers(Iterator start, Iterator end) {
    std::for_each(start, end, [](const auto& number) {
        if (number % 2 == 0) {
            std::cout << number << " ";
        }
    });
}

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    printEvenNumbers(numbers.begin(), numbers.end());

    return 0;
}


All these examples will output the even numbers between 1 and 10: 2 4 6 8 10.

Facebook Twitter LinkedIn Telegram Pocket

Related Posts:

In Jinja2, comparing float numbers can be done using various comparison operators such as &lt;, &gt;, &lt;=, &gt;=, ==, and !=.To compare float numbers in Jinja2, you can directly use these operators in your template along with the float numbers you want to co...
To print your name in C++, you can use the &#34;cout&#34; function along with the stream insertion operator &#34;&lt;&lt;&#34;. Follow these steps:Start by including the necessary header file: #include using namespace std; Create the main function which serves...
To declare a float variable in C++, you would follow the syntax: float variable_name; For example, if you wanted to declare a float variable called &#34;height&#34;, you would write: float height; This statement tells the compiler to allocate memory for a vari...