47.6k views
3 votes
Write a function called printEvens that prints all the even numbers between 2 and 20, inclusive. It will take no arguments and return nothing. You must also use a looping statement in the code to get credit for printing (i.e. no cout << 2 << " " << 4 " " << ...).

User Abhishek J
by
5.3k points

1 Answer

2 votes

Answer:

#include <iostream>

using namespace std;

void printEvens() {

for (int i = 2; i <= 20; i++) {

if (i % 2 == 0)

cout << i << " ";

}

}

int main() {

printEvens();

return 0;

}

Step-by-step explanation:

- Inside the function, initialize a for loop that goes from 2 to 20.

- Inside the for loop, check if the numbers are divisible by 2. If yes, print the number

- Inside the main, call the function

User Nolequen
by
5.1k points