Showing posts with label Palindrome Number. Show all posts
Showing posts with label Palindrome Number. Show all posts

Saturday, July 13, 2019

Find All Palindrome Numbers from an Array in Java

In this article, we will see how to find all palindrome from an array. This is a very basic questions in interview, the interviewer will ask the same questions in different way. So, its good to know all possible questions from palindrome. Also there is a question to find all palindrome number from a list.  Find few more collection interview question.  Today we will see how to check a number is palindrome or not. 

FindAllPalindrome.java

package com.techbyteslearn.lab.basic;

public class FindAllPalindrome {
public static void main(String[] args) {
int numberArray[] = { 120, 990, 121, 777, 808, 1280 };
for (int i = 0; i < numberArray.length; i++) {
printOnlyPalindrom(numberArray[i]);
}
}
private static void printOnlyPalindrom(int number) {
int finalNumber = 0;
int oldNumber = number;
// Repeat the loop until the number became zero.
while (number != 0) {
// Get the First Digit (i.e. 1)
int firstDigit = number % 10;
// Get the Result number.
finalNumber = (finalNumber * 10) + firstDigit;
// Now get the remaining digits , after finding the first digit
number = number / 10;
}
// Now compare the finalNumber and oldNumber both are same or not.
if (finalNumber == oldNumber)
System.out.println(finalNumber + " is a Palindrome.");
}
}

Output -

121 is a Palindrome.
777 is a Palindrome.
808 is a Palindrome.

How to Check Whether a Number Is a Palindrome in Java


In this article, we will see how to check if a number is palindrome or not. This is a very basic questions in interview. But, you never know about what kind of question the interviewer will ask. So, better you prepare for every certain questions. Also there is a question to find all palindrome number from a list.  Find few more collection interview question.  Today we will see how to check a number is palindrome or not. 


Palindrome.java

package com.techbyteslearn.lab.basic;

public class Palindrome {

    public static void main(String[] args) {
        int number = 121;
        int temp = number;
        int finalNumber = 0;

        // Repeat the loop until the number becomes zero.
        while (number != 0) {

            // Get the last digit.
            int lastDigit = number % 10;

            // Build the reversed number.
            finalNumber = (finalNumber * 10) + lastDigit;

            // Remove the last digit from the number.
            number = number / 10;
        }

        // Compare the reversed number with the original number.
        if (finalNumber == temp) {
            System.out.println("This number is a Palindrome.");
        } else {
            System.out.println("This number is not a Palindrome.");
        }
    }
}

Output:

This number is a Palindrome.
 
 

One important correction from the original comments: % 10 gets the last digit, not the first digit. Also, number has already become 0 by the time of the final comparison, so comparing with temp is the correct approach.



Happy Learning.