Object-Oriented Programming with Java, part I – Exercise 29

This program displays up the numbers up to and including 100 from the number 2.

My Code

public class EvenNumbers {

    public static void main(String[] args) {
        // Write your code here
        int number = 0;

        while (number < 100) {
            number = number + 2;
            System.out.println(number);
        }
    }
}

Model Code

public class EvenNumbers {

 

    public static void main(String[] args) {

        // Write your code here

        int number = 2;

 

        while (number <= 100) {

            System.out.println(number);

            number = number + 2;

        }

 

        /* also the following would be possible. Here we go through all the numbers trom 1 to 100

         * but print only those that are even

        int number = 1;

 

        while (number <= 100) {

            if (number % 2 == 0) {

                System.out.println(number);

            }

            

            number++;

        }

        */

 

    }

}

Comments

Mine is pretty much the same although i do like the second example of model code where it uses modulo to test for even numbers.

Leave a comment