Practice pieces


Loops: odd/even

This practice piece is all about loops and if-statements.


Hints:

You just need one, main, class to do this job.

The algorithm is:

// Make a loop that runs from one up to and including six. Note that we're not starting at zero.
// In the loop, have an if-else statement that decides if the current loop counter is odd or even.
// In the if and else blocks, print the loop counter and words to say whether it is odd or even.

Note that the way to tell whether something is odd or even is to use the modulus operator "%". This tells you the remainder of an integer division of the number, so, for example "7%3" is 1.

Note also that to print a variable's content and other things together, you need a "+" between them, thus:

int number = 10;
System.out.println("hi, the value is " + number);

Note the use of double quotes.

If you need more help, here's the answers (or one option, anyhow).