Palindrome program is a frequently asked programming interview question to both freshers or experienced in interviews. We will going to learn that in this post.
Let’s start with basics first. We must be aware what is Palindrome.
We read a word or group of words from left to right. If we can read them from right to left same as we read left to right , is called a palindrome.
Examples: “Refer” , “level” , “Madam” , “Nurses run” , 1991 are palindrome.
A number can also be a palindrome number as shown above in example. In this post, we will be learn about a palindrome number. In next post, we will learn about string palindrome.
A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.
For example: 16461.
For examples:
a. 13/10 : Remainder= 3 which is last digit in number 13.
b. 100/10 : Remainder= 0 which is last digit in number 10.
5. New number which is actually reverse number, will be formed by formula “(reverseNumber*10) + remainder”.
6. Now divide the original number by 10 to remove last digit of it.
7. Repeat above steps till original number is less than zero.
package NumberSeries; import java.util.Scanner; public class PalindromeNumber { public static void main(String[] args) { // Taking input from user Scanner sc = new Scanner(System.in); System.out.println("Please input the number to find if it is palindrome or not:"); int inputByUser = sc.nextInt(); System.out.println("Input Number to be checked for palindrome: " + inputByUser); // Closing input stream sc.close(); // Copy input number to a temporary variable to keep original value intact int temp = inputByUser; int revNumber = 0; // checking if number is negative if (inputByUser < 0) { System.out.println("Negative number.Enter positive number."); System.exit(1); } // checking if number is single digit only else if (inputByUser >= 0 && inputByUser <= 9) { System.out.println(inputByUser + " is palindrome as it is single digit number."); System.exit(1); } else { while (temp > 0) { // extracting last digit of number int rem = temp % 10; // forming number revNumber = revNumber * 10 + rem; // removing last digit from number temp = temp / 10; } System.out.println("Input By User:" + inputByUser); System.out.println("Reverse number:" + revNumber); // Comparing if input number and reversed number are same if (inputByUser == revNumber) { System.out.println(inputByUser + " is a Palindrome Number"); } else { System.out.println(inputByUser + " is not a Palindrome Number"); } } } }
Output:
You can run above program for multiple inputs and if it fails for any condition, let me know.
#HappyCoding
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
Hello Folks, You might see below dialog box when you download any file in Chrome browser and it finds that…
Hello Folks, Every browse has its default download directory. Whenever you download a file, it gets downloaded in default download…
Hello Folks, In previous post, we have seen that how can we categorised HTTP methods in Safe and Unsafe methods.…
This program was asked in Siemens. Problem Statement: You need to find missing alphabets from a given string. E.g. User…
Hello guys, In previous post, we have seen Introduction of HTTP methods. In this post, we will see what is…
Let's start with a real time example. You must have heard about Facebook. To create an account of Facebook, you need…