mercredi 3 novembre 2021

How can I make it so my while-loop only prints even numbers? Java Eclipse IDE

Beginner here. For my coding class, we have an assignment that requires us to print numbers 1-20, but configure it so that it only outputs even numbers. Here is what I have so far but I'm quite stuck. He says to put an if statement and use the "%" operator but I've no idea where to put them.

    int counter = 1;
    System.out.println("Part 2 - Even Numbers");
    while (counter <= 20)
    {
        //if (counter 
        System.out.printf("%d ", counter);
        counter++;
    } // end while loop

Instructions for assignment

My Output

CORRECT Output

Aucun commentaire:

Enregistrer un commentaire