It is necessary to create a program that displays the digits of the number entered from the keyboard, separated by a space. For example, for 4561 you need to display 4 5 6 1 .

It is allowed to use while , for , do while and arithmetic operations. String operations are not allowed (for example, toString ).

In the reverse order it was done with the help of this algorithm:

 Scanner s = new Scanner(System.in); int a = s.nextInt(); int b = 0; int c; while (a != 0) { c = a % 10; b = b * 10 + c; a = a / 10; System.out.print(b % 10 + " "); } 

How to display numbers in direct order, taking into account the specified restrictions?

Closed due to the fact that the essence of the question is unclear by the participants Mikhail Vaysman , Artem Konovalov , user194374, Denis Bubnov , aleksandr barakin 20 Feb '17 at 14:56 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    1 2 3 4 5 6 this is for what input value? - default locale
  • 2
    Very interesting you are telling! And the question is what? - VladD
  • a.toString () - and bite one character at a time ... - Akina
  • The question is to output a number through a space, for example, 4561 should be output 4 5 6 1 - Sasha 654
  • while, for, do while and arithmetic operations. I cannot use toString () - Sasha 654

3 answers 3

Option with a preliminary count of the number of digits. Counting the number here is conditional, because in fact it is calculated from which divider to begin to divide the number.

 Scanner s = new Scanner(System.in); int a = s.nextInt(); int divider = 1; while (divider <= a) { divider *= 10; } divider /= 10; while (divider > 0) { int digit = a / divider; a -= digit * divider; System.out.print(digit + " "); divider /= 10; } 

This option only works with positive numbers.


Option without preliminary counting the number of users and with enumeration of all options for the divisor:

 Scanner s = new Scanner(System.in); int a = s.nextInt(); boolean gotFirstDigit = false; for (int divider = 1000 * 1000 * 1000; divider > 0; divider /= 10) { int digit = a / divider; if (!gotFirstDigit) { gotFirstDigit = (digit != 0); } if (gotFirstDigit) { a -= digit * divider; System.out.print(digit + " "); } } 

This option also works with positive numbers only.


Both options use the same way to get numbers: the integer part of dividing a number by a given divisor.


To handle zero and negative numbers, you can add the following code:

 int a = s.nextInt(); if (a == 0) { System.out.print(0); return; } if (a < 0) { System.out.print("- "); a *= -1; } 

The only restriction is that this code will not work for Integer.MIN_VALUE ( -2147483648 ).

    Option using the toCharArray () method:

     public static void main(String[] args) { Scanner number = new Scanner (System.in); // Π²Π²ΠΎΠ΄ΠΈΠΌ число Π² консоли char [] numeral = number.nextLine().toCharArray(); // ΠΈΠ· Π²Π²Π΅Π΄Π΅Π½Π½ΠΎΠ³ΠΎ числа создаСм массив символов for (char x:numeral) System.out.print(x + " "); // ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ элСмСнт массива Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π² консоль Ρ‡Π΅Ρ€Π΅Π· ΠΏΡ€ΠΎΠ±Π΅Π» // Ρ€Π°Π·Π²Π΅Ρ€Π½ΡƒΡ‚ΠΎ: Scanner num1 = new Scanner (System.in); System.out.println("\nEnter number:"); String number1 = num1.nextLine(); char [] numeral1 = number1.toCharArray(); for (int x = 0; x < numeral1.length; x++) System.out.print(numeral1 [x] + " "); num1.close(); } Console: 4485455 4 4 8 5 4 5 5 Enter number: 548452 5 4 8 4 5 2 

      Recursive implementation:

       public class Task { private int number; public Task(int number) {this.number = number;} public void printSpaced() {spaced(number);} private void spaced(int number) { if (number < 0) {System.out.print("-");spaced(-number);return;} // Support negative if (number >= 10) spaced(number/10); System.out.print(number%10 + " "); } }