0
Print even numbers between x&y in descending order
4 Respostas
0
for (int i = x+1; x < y; y++)
{
if(i%2 == 0)
cout << i << endl;
}
0
used some techniques.
http://www.sololearn.com/app/cplusplus/playground/cKuzdyV4Qg28/
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "enter x and y:";
cin >> x >> y;
// if x > y then x else y
int bigger = x > y ? x : y;
// if bigger == x then y else x
int smaller = bigger == x ? y : x;
for (int i = bigger - 1; i > smaller; --i)
//using this clue:
//if i is odd, i&1 is 1
//else 0
if (!(i & 1))
cout << i << endl;
return 0;
}
0
kiwiyou's answer is right but is has a tiny mistake in the last part. If I is odd, then i%2 ==1.
You can just use this instead.:
if(i%2==0)
{std::cout<< i << "\n";}
0
@Petr Dron
I didn't use modulo operator. haha
I used bitwise and operator.
And even if I used modulo operator, it was not a mistake.