Check Whether a Number is Palindrome or Not in Java

Exercise:

Write a Java Program to check whether a number is palindrome or not.

1.Check Using While loop

Click Here to View the Solution!
public class PalindromeChecker {
 
    public static void main(String[] args) {
 
        int num = 14141, reversedInt = 0, remainder, originalInt;
 
        originalInt = num;
 
        // reversed integer is stored in variable reversedInt
        while( num != 0 )
        {
            remainder = num % 10;
            reversedInt = reversedInt * 10 + remainder;
            num  /= 10;
        }
 
        // checks if orignalInteger and reversedInteger are equal
        if (originalInt == reversedInt)
            System.out.println(originalInt + " is a palindrome.");
        else
            System.out.println(originalInt  + " is not a palindrome.");
    }
} 
Click Here to View the Output!
14141 is a palindrome.
Click Here to View the Explanation!
  • This program is used to check whether a number is a palindrome or not by using a while loop.
  • Initially, four integer variables are initialized as num = 14141, reversedInteger = 0, remainder and originalInt. The value in the variable num is stored in another variable originalInt so that after the reverse of num is found, the comparison can be done between the original and reversed number.
  • A condition is then set in a while loop that states that the iterations will continue till num becomes equal to zero (num != 0).
  • In the body of the while loop, the remainder of num is calculated by dividing with 10 and is then stored in the variable remainder.
  • This remainder value is then added to the value of reversedInt after multiplying the reversedInt with 10. So that the remainder value adds up to the next place value.
  • The value of the variable num is then updated by dividing it by 10, thus, removing the last digit of the num value.
  • The loop will continue until the value of num becomes zero.
  • After the loop ends, both the values stored in the variable originalInt and reversedInt are compared so as to determine that whether num is a palindrome or not.
  • Lastly, the output is displayed as follows: 14141 is a palindrome number.

2.Check Using While loop

Click Here to View the Solution!
public class Palindrome {
 
    public static void main(String[] args) {
 
        int num = 766567, reversedInt = 0, remainder, originalInt;
 
        originalInt = num;
 
        // reversed integer is stored in variable
        for( ;num != 0; num /= 10 )
        {
            remainder = num % 10;
            reversedInt = reversedInt * 10 + remainder;
        }
 
        // palindrome if orignalInt and reversedInt are equal
        if (originalInt == reversedInt)
            System.out.println(originalInt + " is a palindrome.");
        else
            System.out.println(originalInt + " is not a palindrome.");
    }
} 
Click Here to View the Output!
766567 is not a palindrome.
Click Here to View the Explanation!
  • This program is used to determine whether a number is a palindrome or not by using a for loop.
  • Initially, four integer variables are initialized as num = 766567, reversedInt = 0, remainder and originalInt.
  • Instead of using the while loop as the above program, this program uses a for loop which holds no initialization, a condition (num != 0) and an  expression that will update the value of num (num/10). This states that in every iteration, both the conditions, will be checked simultaneously before the execution of the body of the loop.
  • In the body of the for loop, same steps will be followed as that of the above program for finding out the value of reverseInt.
  • After the loop ends, both the values stored in the variable originalInt and reversedInt are compared so as to determine whether num is a palindrome or not.
  • Lastly, the output is displayed as follows: 766567 is not a palindrome number.

%d bloggers like this: