Need some help with this !

So this question probably sounds and is stupid , but i just started learning c++ in school. So we're learning about the while command. And i was doing my homework when i ran in to this problem.
My task is this: Count all the even numbers starting from 10 backwards. I just tried this and i don't know why it won't work . Any help would be much appriciated .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <cstdlib>
#include <iostream>
using namespace std;
int main ()
{
    int a;
    cout<<"Od koj broj da pocnam da brojam nanazad?"<<endl;
    cin>>a;
    while ( a % 2 == 0)
    {
        cout<< a <<endl;
        a--;
    }
    return 0;
}
While loops as long as the condition remains true.

Your condition has exactly two logical states: a is odd or even.

If a is odd, then condition is immediately false and execution moves to line 14.
If a is even, then it is shown, a decrements to odd value, and then (see the case above).


If you want to count down, then the condition in while should be something that remains true until a becomes small enough, ie while a is larger than some limit.

Perhaps you need an IF inside the loop to print just the even values?


(There is a different method too: first make even and then loop, decrementing by two at a time.)
Thanks a lot, really helped me a lot. I made it work this way ..

#include <cstdlib>
#include <iostream>
using namespace std;
int main ()
{
int a;
cin>>a;
while ( a > 0)
{

if(a%2==0)
cout<<a<<endl;
a--;

}
return 0;
}
Topic archived. No new replies allowed.