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

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

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  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


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.