0
if I want to print ten number by identifying whether it is odd or even by using for loop. how to do?
2 odpowiedzi
+ 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;
}
}
}
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;
}