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.
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:
- 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; } |
- 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; } |
- 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; } |
- 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
.