Skip to Content

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

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  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. */