I am trying to write a program that prints out the even numbers between 1 and 21 using the WHILE loop. This is what I have, but there is no output. Why is that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main()
{
int number = 1;
while (number <= 21 && number / 2)
{
cout << number;
number++;
}
return 0;
}
1. The reason nothing is being outputted is that, number starts at 1, the condition is not met therefore it will never enter the while loop, and therefore it will never become 2, which would make the condition true.
I was trying to say when the number was even
2. You want to use the modulus operator here, otherwise you will get incorrect results -