Palindrome Number in Java

In this post, we will discuss Palindrome Number in Java along with code too. So, let’s start without wasting time.

Introduction

In math, a palindrome is a number that reads the same forward and backward. For example, 353, 787, and 2332 are examples of palindromes. By definition, all numbers that have the same digits such as 4, 11, 55, 222, and 6666 are examples of palindromes.

For instance, 121, can be read backward or forwards. Palindromic numbers are very easy to create from other numbers with the aid of addition.

Try this:

  1. Write down any number that is more than one digit. (e.g. 47)
  2. Write down the number reversed beneath the first number. (47+74)
  3. Add the two numbers together. (121)
  4. And 121 is indeed a palindrome.

Try a simple one first, such as 18. Sometimes you need to use the first addition answer and repeat the process of reversing and adding.

#1 Palindrome Code in Java – Number

class Main {
  public static void main(String[] args) {
    
    int num = 5665, reversedNum = 0, remainder;
    
    // store the number to originalNum
    int originalNum = num;
    
    // get the reverse of originalNum
    // store it in variable
    while (num != 0) {
      remainder = num % 10;
      reversedNum = reversedNum * 10 + remainder;
      num /= 10;
    }
    
    // check if reversedNum and originalNum are equal
    if (originalNum == reversedNum) {
      System.out.println(originalNum + " is Palindrome.");
    }
    else {
      System.out.println(originalNum + " is not Palindrome.");
    }
  }
}

Here, we take the number 5665. We use the while loop to check the condition i.e till the num!=0. We pass the number through the loop and separate the unit digit forming a number that will be exactly reversed of the existing number.

The reversed number is then compared to the initial number. If the numbers are same, then it is concluded that it is a Palindrome else it is not!

#2 Palindrome Code in Java – String

class Main {
  public static void main(String[] args) {

    String str = "GeeG", reverseStr = "";
    
    int strLength = str.length();

    for (int i = (strLength - 1); i >=0; --i) {
      reverseStr = reverseStr + str.charAt(i);
    }

    if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
      System.out.println(str + " is a Palindrome String.");
    }
    else {
      System.out.println(str + " is not a Palindrome String.");
    }
  }
}

Output

GeeG is a Palindrome String.

In the above example, we have a string “GeeG” stored in str. Here, we have used the

  1. for loop to reverse the string

The loop runs from the end to the beginning of the string.
The charAt() method accesses each character of the string.
Each character of the string is accessed in reverse order and stored in reverseStr.

  1. if statement to compare str and reverseStr

The toLowerCase() method converts both str and reverseStr to lowercase. This is because Java is case sensitive and ‘r’ and ‘R’ are two different values.
The equals() method checks if two strings are equal.

Conclusion

In this post, we have studied palindrome number in java and how to code check one using Java!

  1. Class and Object in Java
  2. Input Output In Java
  3. Identifiers and Variables Explained
  4. Data Types In Java

Leave a Comment

Your email address will not be published. Required fields are marked *