Read Number from Console and Check if it is a Palindrome Number

  1. /*
  2.   Read Number from Console and Check if it is a Palindrome Number
  3.   This Java example shows how to input the number from console and
  4.   check if the number is a palindrome number or not.
  5.  */
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10.  
  11. public class InputPalindromeNumberExample {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. System.out.println("Enter the number to check..");
  16. int number = 0;
  17.  
  18. try
  19. {
  20. //take input from console
  21. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22. //parse the line into int
  23. number = Integer.parseInt(br.readLine());
  24.  
  25. }
  26. catch(NumberFormatException ne)
  27. {
  28. System.out.println("Invalid input: " + ne);
  29. System.exit(0);
  30. }
  31. catch(IOException ioe)
  32. {
  33. System.out.println("I/O Error: " + ioe);
  34. System.exit(0);
  35. }
  36.  
  37. System.out.println("Number is " + number);
  38. int n = number;
  39. int reversedNumber = 0;
  40. int temp=0;
  41.  
  42. //reverse the number
  43. while(n > 0){
  44. temp = n % 10;
  45. n = n / 10;
  46. reversedNumber = reversedNumber * 10 + temp;
  47. }
  48.  
  49. /*
  50. * if the number and it's reversed number are same, the number is a
  51. * palindrome number
  52. */
  53. if(number == reversedNumber)
  54. System.out.println(number + " is a palindrome number");
  55. else
  56. System.out.println(number + " is not a palindrome number");
  57. }
  58.  
  59. }
  60.  
  61. /*
  62. Output of the program would be
  63. Enter the number to check..
  64. 121
  65. Number is 121
  66. 121 is a palindrome number
  67. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!