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

This program types all the whole numbers from 1 to the target number and then stops.

My Code

import java.util.Scanner;


public class UpToCertainNumber {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        
        // Write your code here
        int output = 1;
        System.out.print("Up to what number? ");
        int number = Integer.parseInt(reader.nextLine());
        
        while (output <= number){
            System.out.println(output);
            output++;
        }
        
    }
}

Model Code

import java.util.Scanner;

 

 

public class UpToCertainNumber {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        

        // Write your code here

        

        System.out.print("Up to what number? ");

        int last = Integer.parseInt(reader.nextLine());

        

        int number = 1;

        while (number <= last) {

            System.out.println(number);

            number++;

        }

    }

}

Comments

Same as mine

Leave a Reply

Your email address will not be published. Required fields are marked *