Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Program 01: Java Program to Check If a Given Number is Palindrome | Make Selenium Easy

Posted on 08/27/2018 By admin

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.

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.

Logic to find out a numeral palindrome:

    1. We need to extract last digit from input number and keep it at first position for a new number.
    2. Extract the second last digit and put in the next position of new number.
    3. We can achieve that using multiple ways like reverse traversing or charAt methods etc. Here we will see include some number concepts.
    4. We can extract last digit of a number(remainder) if we divide number by 10.

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  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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 45: Passing Parameters at Test Method Level in TestNG | Make Selenium Easy
Next Post: Java Programs 15: Java Program to Print Fibonacci Series at Given Position Using Recursion | Make Selenium Easy

Related Posts

Git Tutorial 4 – Create a new Git repository – git init Command Uncategorized
image – Make Selenium Easy Uncategorized
July 13, 2018 – Make Selenium Easy Uncategorized
Frequently Asked Java Program 03: Java Program to check if any string is palindrome Using inbuilt Reverse method of Java Uncategorized
SelectPost – Make Selenium Easy Uncategorized
Postman Tutorial Part 44 – Data Driven Testing in Postman Using CSV File Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark