Skip to content

Instantly share code, notes, and snippets.

@eMahtab
Created November 19, 2015 07:22
Show Gist options
  • Save eMahtab/47b863ee2140266cf62e to your computer and use it in GitHub Desktop.
Save eMahtab/47b863ee2140266cf62e to your computer and use it in GitHub Desktop.
Java Program to check whether a number is palindrome or not. e.g. 121 is palindrome as If we reverse the order of digits, its the same number
import java.util.Scanner;
import java.util.InputMismatchException;
public class PalindromeNumberCheck{
public static void main(String args[]){
long number=-999,copyOfNumber=-999;
System.out.print("Enter a Number : ");
Scanner sc=new Scanner(System.in);
try{
number=sc.nextLong();
copyOfNumber=number;
}catch(InputMismatchException ime){
System.err.println("Error : Please eneter a valid number");
System.exit(1);
}
long remainder=0;
String str="";
while(number >0){
remainder=number%10;
str=str+remainder;
number=number/10;
}
if(Long.parseLong(str)==copyOfNumber){
System.out.println("Number "+copyOfNumber+" is Palindrome");
}else{
System.out.println("Number "+copyOfNumber+" is not a Palindrome");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment