-1

My code basically arranged the number into reverse order for example 415 the program will arrange it into 514 well my code is correct but I have a problem the output should be vertical.

expected output
5
1
4
import java.util.Scanner;
public class Main{
  public static void main(String args[])
  {
    Scanner in = new Scanner(System.in);
    int num = in.nextInt();
    int rev=0;
    while( num != 0 )
      {
          rev = rev * 10;
          rev = rev + num%10;
          num = num/10;
      }
          System.out.println(rev);
  }
}
Eklavya
  • 17,618
  • 4
  • 28
  • 57

2 Answers2

5

You just need to do it like this:

while( num != 0 )
{
    System.out.println(num % 10);
    num = num / 10;
}
VFX
  • 496
  • 4
  • 13
0

I am leaving this as an alternate method. By this you can take int which are are too big to fit in int datatype

Scanner in = new Scanner(System.in);
String s = in.nextLine();
for (int i = s.length() - 1; i >= 0; i--) {
    System.out.println(s.charAt(i));
}
Lovesh Dongre
  • 1,294
  • 8
  • 23