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

This program uses a while loop to print out all the numbers from 1 to 100.

My Code

public class FromOneToHundred {

    public static void main(String[] args) {
        // Write your program here
        int number = 0;
        
        while (number < 100){
            number ++;
            System.out.println(number);
        }
    }
}

Model Code

public class FromOneToHundred {

 

    public static void main(String[] args) {

        // Write your program here

        int number = 1;

        

        while (number <= 100) {

            System.out.println(number);

            number++;

        }

    }

}

Comments

Pretty much the same although a good example of how code changes depending on where you start from. I intialised the int at 0 which changed where number++ went and what the true statement inside the while loop was.

Leave a comment