Print even numbers between x&y in descending order | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Print even numbers between x&y in descending order

28th Sep 2016, 1:54 PM
rounak
4 Answers
0
for (int i = x+1; x < y; y++) { if(i%2 == 0) cout << i << endl; }
28th Sep 2016, 2:05 PM
Łukasz Walancik
Łukasz Walancik - avatar
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; }
29th Sep 2016, 8:27 AM
kiwiyou
kiwiyou - avatar
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";}
29th Sep 2016, 7:54 PM
Petr Dron
Petr Dron - avatar
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.
30th Sep 2016, 6:33 AM
kiwiyou
kiwiyou - avatar