0

if I want to print ten number by identifying whether it is odd or even by using for loop. how to do?

30th Aug 2016, 7:52 AM
dharmesh
2 Respuestas
+ 1
Use modulus (%) to determine whether a number is even or odd. It gives you the remainder of the euclidian division. In particular, a%b is 0 if a is a muliple of b, and a%10 gives you the last digit of a. Here is what you asked for, but I doubt this was exactly what you meant: int count = 0; for(int i = 0; true; i++) { if (i % 2 == 0) { cout << i << endl; count++; if (count >= 10) { break; } } }
30th Aug 2016, 8:45 AM
Zen
Zen - avatar
0
Print out num++ inside the loop instead of ++num: For instance: #include <iostream> using namespace std intmain() { int num = 1; while (num =< 10) { cout<<num++<<endl;++num; }
30th Aug 2016, 4:45 PM
Rafael Figueroa
Rafael Figueroa - avatar