fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.Scanner;
  8. class ForLoopReverseDemo
  9. {
  10. public static void main(String args[])
  11. {
  12. int num=0;
  13. int reversenum =0;
  14. System.out.println("Input your number and press enter: ");
  15. //This statement will capture the user input
  16. Scanner in = new Scanner(System.in);
  17. //Captured input would be stored in number num
  18. num = in.nextInt();
  19. /* for loop: No initialization part as num is already
  20.   * initialized and no increment/decrement part as logic
  21.   * num = num/10 already decrements the value of num
  22.   */
  23. for( ;num != 0; )
  24. {
  25. reversenum = reversenum * 10;
  26. reversenum = reversenum + num%10;
  27. num = num/10;
  28. }
  29.  
  30. System.out.println("Reverse of specified number is: "+reversenum);
  31. }
  32. }
Runtime error #stdin #stdout #stderr 0.1s 3359744KB
stdin
Standard input is empty
stdout
Input your number and press enter: 
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at ForLoopReverseDemo.main(Main.java:18)