Write a Java program that prompts the user to input an integer and then outputs the number with the digits reversed order


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2.  
  3. public class JavaLoopExcercise
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner console = new Scanner(System.in);
  8.  
  9. int number;
  10. int reverse = 0;
  11.  
  12. System.out.print("Enter the number ");
  13. number = console.nextInt();
  14.  
  15. int temp = number;
  16. int remainder = 0;
  17.  
  18. while(temp>0)
  19. {
  20. remainder = temp % 10;
  21. reverse = reverse * 10 + remainder;
  22. temp /= 10;
  23. }
  24.  
  25. System.out.println("Reverse of " + number + " is " + reverse);
  26. }
  27. }

Result

java techstudy.org

     





Related Topics




Popular Posts