Print even numbers between two integers

Hi,
What's the easiest way to print all even numbers between two ints?
I know it would be a while loop, but I'm not sure how to achieve it.
Thanks

Loop through all the numbers in range [a, b].
See the loops section of the Control flow tutorial: http://www.cplusplus.com/doc/tutorial/control/

The easiest way to check if a number is even is to check if it is divisible by 2.

1
2
if (i % 2 == 0)
    std::cout << i << " is even!\n";
Last edited on
But how would I make it so it outputs all the even numbers between the two integers?
I don't want to just feed you the answer.
You need to make a for loop or while loop that starts at the first number, and ends at the second number. Look at the link I posted -- "Iteration statements (loops)" section.

Post your attempt here, with code, if you are still confused.
Last edited on
To give you an idea.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
	int lowestNum{}, highestNum{};

	std::cout << "Lowest Number: ";
	std::cin >> lowestNum;

	std::cout << "Highest Numbers: ";
	std::cin >> highestNum;

	for (int count = lowestNum; count <= highestNum; count++)
	{
		if (count % 2 == 0)
			std::cout << count << std::endl;
	}
}
I got it thank you I was looking at the wrong section on the link
Topic archived. No new replies allowed.