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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative