Dec 10, 2018 at 5:46pm Dec 10, 2018 at 5:46pm UTC
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
Dec 10, 2018 at 5:55pm Dec 10, 2018 at 5:55pm UTC
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 Dec 10, 2018 at 5:55pm Dec 10, 2018 at 5:55pm UTC
Dec 10, 2018 at 6:07pm Dec 10, 2018 at 6:07pm UTC
But how would I make it so it outputs all the even numbers between the two integers?
Dec 10, 2018 at 6:09pm Dec 10, 2018 at 6:09pm UTC
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 Dec 10, 2018 at 6:10pm Dec 10, 2018 at 6:10pm UTC
Dec 10, 2018 at 6:17pm Dec 10, 2018 at 6:17pm UTC
I got it thank you I was looking at the wrong section on the link