Answer the question
In order to leave comments, you need to log in
Why doesn't the code (C++) work?
should display numbers that are divisible by 2, but not divisible by 4. But in the end, on the contrary: it displays only those that are divisible by 4,
the essence:
for(x=0;x<100;x=x+2)
{
if(x%4) continue;
cout<<x;
}
Answer the question
In order to leave comments, you need to log in
if(x%4) continue; What do you think it does? And, in particular, what would 5%4 be?
It is possible like this:
for(int x = 0; x < 100; x = x + 2)
{
if(x % 4) cout << x;
}
for(int x = 0; x < 100; x = x + 2)
{
if(x % 4 == 0) continue;
cout << x;
}
for(int x = 2; x <= 100; x += 4)
{
cout << x;
}
int main() {
int x;
cin >> x;
if (x % 2 == 0 && x % 4 != 0) {
cout << x << endl;
}
}
For the code to work correctly, it is enough just to invert the condition, because 0 in C++ is false, and all non-zero values are true.
for (x = 0; x < 100; x = x + 2)
{
if ( !(x % 4) ) continue;
cout << x;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question