Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Write a program to check whether number is palindrome or not

  1. /*
  2.  * Write a program to check whether number is palindrome or not. Number should
  3.  * be read from console. Number is considered to be palindrome if number
  4.  * and it's reversed number are same.
  5.  */
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10.  
  11. public class PalindromeCheck {
  12.  public static void main(String args[]){
  13.  
  14.   System.out.println("Enter number");
  15.  
  16.   try{
  17.    
  18.    //read the number from console
  19.    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  20.    int n = Integer.parseInt(br.readLine());
  21.    
  22.    //first, we will reverse the number
  23.    int number = n;
  24.    int reversedNumber = 0;
  25.    int t =0 ;
  26.    
  27.    while(number > 0){
  28.     t = number % 10;
  29.     number = number / 10;
  30.     reversedNumber = reversedNumber * 10 + t;
  31.    }
  32.    
  33.    System.out.println("Reversed number is " + reversedNumber);
  34.    
  35.    if(n == reversedNumber)
  36.     System.out.println("Number is palindrome");
  37.    else
  38.     System.out.println("Number is not palindrome");
  39.    
  40.   }catch(NumberFormatException nef){
  41.    System.out.println("Invalid number.");
  42.   }catch(IOException ioe){
  43.    System.out.println("IO error" + ioe);
  44.   }
  45.  
  46.  }
  47. }
Your rating: None Average: 2.3 (3 votes)
Share this