Skip to content
Advertisement

Ask the user for the starting value. Set up the output to display 10 values per line and then start a new line

public class StayPositive {

  public static void main(String[] args) {
    System.out.println("Counting down..");
    int i = 59;
    while (i >= 0) {
      System.out.println(i);
      i--;
    }
  }
}

Advertisement

Answer

You can use Scanner to take user input. And use % function to change the liner after every 10 numbers:

public static void main(String[] args) {
    Scanner scanner =  new Scanner(System.in);
    System.out.print("Give a number: ");
    int givenNumber = Integer.valueOf(scanner.nextLine());

    for(int i = givenNumber, j=1 ; i>=0 ; i--, j++) {
      if(j%10 == 0) {
        System.out.println(i);
      } else {
        System.out.print(i);;
      }
    }
  }

Input : 59

Output :

59585756555453525150
49484746454443424140
39383736353433323130
29282726252423222120
19181716151413121110
9876543210
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement