Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, February 5, 2020

Java Program to Check Whether a Number is Palindrome or Not

1. Introduction


In this tutorial, we will learn how to check whether a given number is a palindrome or not.

Definition: A palindrome is a word, number, phrase, or other sequences of characters that reads the same backward as forward, such as madam, racecar.
Palindrome ref

But in out case input is a number such as 121, 345543 are examples of the palindrome.

To understand this problem, you must have knowledge on reversing a number in java.

You can achieve and implement in many ways using for loop and while loop.

A) Number Palindrome check using For loop
B) Number Palindrome check using while loop
C) Number Palindrome check with StringBuffer.reverse()

In approach A and B, We need to reverse the number using '%' and '/' operators and compares the given number with the reversed number. If both are the same then it is a palindrome, otherwise, it is not.





2) Number Palindrome check using For loop


package com.javaprogramto.w3schools.engineering.programs;

import java.util.Scanner;

/**
 * 
 * Number Palindrome check using For loop
 * 
 * @version JavaProgramTo.com
 */
public class NumberPalindromeForLoop {

    public static void main(String[] args) {

        System.out.println("Enter a number : ");
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();

        int originalNumber = number;
        int reminder = 0;
        int reversedNumber = 0;
        for (; number != 0;) {

            reminder = number % 10;
            reversedNumber = reversedNumber * 10 + reminder;
            number = number / 10;
        }

        System.out.println("Reversed Number : " + reversedNumber);

        if (originalNumber == reversedNumber) {
            System.out.println("Number " + originalNumber + " is a palindrome");
        } else {
            System.out.println("Number " + originalNumber + " is not a palindrome");
        }
    }
}

Output:

Enter a number : 1324234
Reversed Number : 4324231
Number 1324234 is not a palindrome

3) Number Palindrome check using While loop


package com.javaprogramto.w3schools.engineering.programs;

import java.util.Scanner;

/**
 * 
 * Number Palindrome check using For loop
 * 
 * @version JavaProgramTo.com
 */
public class NumberPalindromeWhileLoop {

    public static void main(String[] args) {

        System.out.println("Enter a number : ");
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();

        int originalNumber = number;
        int reminder = 0;
        int reversedNumber = 0;
        while (number != 0) {

            reminder = number % 10;
            reversedNumber = reversedNumber * 10 + reminder;
            number = number / 10;
        }

        System.out.println("Reversed Number : " + reversedNumber);

        if (originalNumber == reversedNumber) {
            System.out.println("Number " + originalNumber + " is a palindrome");
        } else {
            System.out.println("Number " + originalNumber + " is not a palindrome");
        }
    }
}

The above While loop example program also produces the same output as in with for loop. For loop does not look readable because we are not passing values in the initialization and increment/decrement section.

4. Number Palindrome check with StringBuffer.reverse() 


Let us try the different approach that converting the number into String first then use the reverse() function.

package com.javaprogramto.w3schools.engineering.programs;

import java.util.Scanner;

/**
 * 
 * Number Palindrome check using StringBuffer.reverse() method.
 * 
 * @version JavaProgramTo.com
 */
public class NumberPalindromeStringBuffer {

    public static void main(String[] args) {

        System.out.println("Enter a number : ");
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();

        StringBuffer buffer = new StringBuffer(String.valueOf(number));
        StringBuffer reversedBuffer = buffer.reverse();

        if (buffer.toString() == reversedBuffer.toString()) {
            System.out.println("Number " + buffer.toString() + " is a palindrome");
        } else {
            System.out.println("Number " + buffer.toString() + " is not a palindrome");
        }
    }
}

5. Conclusion


In this article, We have seen how to check the number palindrome in 3 ways. All three approaches will produce the same output but using for loop does not look code is more readable and StringBuffer usage creates additional objects in the memory. Palindrome check using while loop is simple and does not use any java API builtin methods. This code is most efficient.

Refer to the String Palindrome check in Java. This article has the best approach to check String is palindrome or not.


No comments:

Post a Comment

Please do not add any spam links in the comments section.