DEV Community

Ajay k
Ajay k

Posted on • Edited on

Check the given number is palindrome or not

Today we can see how to solve

Check the given number is palindrome or not

we can solve this using
1. mod method
2. String builder reverse method

A number is palindrome if number and the reverse of given number is same
for 121 the reverse of 121 is again 121 so its palindrome
for 123 the reverse of 123 is 321 321 is not equal to 123
,so it's not palindrome

  1. Mod Method
boolean usingModMethod(int inputNumber) {
    int reverseNumber = 0;
    int temp = inputNumber;
    while (temp > 0) {
      int lastDigit = temp % 10;
      reverseNumber = reverseNumber * 10 + lastDigit;
      temp=temp/10;
    }
    if (reverseNumber == inputNumber) {
      return true;
    }
    return false;
  }
Enter fullscreen mode Exit fullscreen mode

Here we are going to check if the given number is a single digit number because even if we reverse 7 its going to give 7 only so we are checking that

if the number is not a single digit we are going to create a reverseNumber variable with 0 as initial value,Then we create a temp value and assign the input value to it

we create a while loop which is going to check is the given number is greater then Zero(0),if true we are going to enter the while loop
inside the while loop we take the last digit of the given number, so if the input is 121 and we apply mod (%) 10 to 121 it will give the last digit which is 1

121 % 10 => 1 (last digit)

now we multiply the reverseNumber by 10 and we add out last digit

reverseNumber = ( 0 * 10 ) + 1 => 1

now we divide the temp by 10 so 121 becomes 12

NOTE:- Refer Find the number of digits note section to know about divide in java.

now while loop checks is 12 greater than 0 this again enters while loop we get the last digit by mod

12 % 10 => 2 (last digit)

now we multiply the reverseNumber by 10 and we add out last digit

reverseNumber = ( 1 * 10 ) + 2(last digit) => 12

now we divide the temp by 10 so 12 becomes 1, the while loop checks is 1 greater than 0, so this again enters while loop we get the last digit by mod

1 % 10 => 1 (last digit)

now we multiply the reverseNumber by 10 and we add out last digit

reverseNumber = ( 12 * 10 ) + 1(last digit) => 121

now we divide the temp by 10 so 1 becomes 0, so temp is not greater than 0 and wont enter the while loop

we check is the inputNumber is same as reverseNumber if its same we return true else we return false

  1. Using StringBuilder reverse
boolean usingStringBuilder(int inputNumber){
    String str = String.valueOf(inputNumber);
    StringBuilder br = new StringBuilder(str);
    String reverse = br.reverse().toString();
    if(str.equals(reverse)){
      return true;
    }
    return false;
  }
Enter fullscreen mode Exit fullscreen mode

In This we are using to java StringBuilder reverse method to reverse the number and check is it same as inputNumber

NOTE:- Remember to convert string builder to string before compare or else you will get false always even if it same

This is a part of my DSA preparation series
Day 1 to 9 : Analysis of Algorithm
Day 10 : Find the number of digits
Day 11 : Check the given number is palindrome or not

Top comments (0)