Wednesday, 23 November 2016

Find Palindrome number or not

Palindrome number is a number that remains the same when its digits are reversed. For example, 121 is the palindrome number because the reverse of 121 is the original number. In given program, we have first calculated the reverse of given number. Then check that number with original number. If both are same then the number is palindrome number otherwise not.


PROGRAM
class PalindromNum {

 public static void main(String[] args) {

  int num = Integer.parseInt(args[0]);
  int n = num;
  int rev=0;
  
  while(n > 0) {
   
   rev = rev *10 +(n%10);
   n = n / 10;
  }
  
  if(num == rev) {
   
   System.out.println(num+" == "+rev);
   System.out.println("Hence number is a palindrome");
  }
  else {
   
   System.out.println(num+" != "+rev);
   System.out.println("Hence number is not a palindrome");
  }
 }
}

OUTPUT 1
C:\>javac PalindromNum.java
C:\>java PalindromNum 121
121 == 121
Hence number is a palindrome

OUTPUT 2
C:\>javac PalindromNum.java
C:\>java PalindromNum 456
456 != 654
Hence number is not a palindrome

No comments:

Post a Comment