Palindrome Program in Java: How to Analyze Number or String? 

4 minute read
10 shares
Palindrome Program in Java

You might have heard about the word Palindrome in your school. In the realm of computer programming and technological advancements, everything is programmed to offer high-class services to humans with a single touch. A palindrome number or string can also be identified through programming in Java. A palindrome number/string is simply a number or word whose sequence reads the same backwards as it reads from forward. Java is one of the widely acclaimed programming languages taught in schools and at higher levels of education. You can use this platform to analyze whether the input is palindrome or not. Stay tuned and continue reading this blog to learn about the Palindrome program in Java!

Also Read: Know all about Computer Programming

What is a Palindrome Number?

A palindrome number reads the same from forward and backward (punctuation, space, and capitalization need to be ignored). That means that the palindrome number is the number that remains the same even after reversing the number.

You can understand this through the following examples of Palindrome Numbers:

  • 121
  • 101
  • 131
  • 1221
  • 1661

When you reverse these numbers you will find that they will remain the same so such numbers are referred to as Palindrome.

Also Read: Diploma in Computer Applications

Steps to Check the Palindrome

Source: SDET-QA

Here are some basic steps that the programmer needs to follow:Source: SDET-QA

  1. Ask the user to Input the number/string 
  2. Reverse the entered number/string
  3. Compare the input
  4. Generate the Output

Palindrome Program in Java to Check Whether the Input Number is Palindrome or Not

Here is a Palindrome Program in Java:

import java.util.Scanner;

public class PalindromeNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print(“Enter a number: “);

        int number = scanner.nextInt();

        int originalNumber = number;

        int reversedNumber = 0;

        while (number > 0) {

            int digit = number % 10;

            reversedNumber = reversedNumber * 10 + digit;

            number /= 10;

        }

        if (originalNumber == reversedNumber) {

            System.out.println(originalNumber + ” is a palindrome”);

        } else {

            System.out.println(originalNumber + ” is not a palindrome”);

        }

    }

}

Output

Enter a number: 121

121 is a palindrome

What is a Palindrome String?

Palindrome String refers to the string that is the same after reversing. 

Here are some examples:

  • noon
  • mom
  • nitin
  • racecar
  • madam
  • kanak
  • refer
  • rotor
  • taco cat
  • kayak
  • level
  • Civic

Also Read: Python Vs Java 

Palindrome Program in Java to check a String

Given below a Java program to check whether the entered string is a palindrome or not:

public class Palindrome

{

    public static void main(String args[])

    {

        String x, y = “”;

       Scanner a = new Scanner(System.in);

      System.out.print(“Enter  string you want to check:”);

     x = a.nextLine();

        int l = x.length();

       for(int k = l – 1; k >= 0; k–)

     {

          y = y + x.charAt(k);

      }

      if(x.equalsIgnoreCase(y))

        {

            System.out.println(“The string is palindrome.”);

        }

        else

        {

            System. out.println(“The string is not a palindrome.”);

        }

    }

}

Output

Enter string you want to check: 

Tattarrattat

The String is palindrome.

Relevant Blogs 

What are Human Rights and Why are they important?Engineer’s Day 2023: Theme
Engineer’s Day Celebration IdeasNational Techies Day 2023
Top 10 Funny Engineer’s Day WishesNational Engineer’s Day 2023: 10 Quotes to Relate With!
National Thermal Engineer Day 2023National Engineer’s Day: Memes

FAQs

What is the Palindrome number in Java?

A palindrome number refers to the number that remains the same after reversing. This means if we reverse all the digits of the entered number the number remains unchanged. Example of palindrome number in Java is 121, 131, 1331, 1441, etc.

What is a Palindrome String?

The concept of Palindrome number is also applicable to Palindrome string. It is the string of letters that remains the same even after reversing.

How to check if a word is Palindrome in Java?

Various method to check Palindrome in Java is the Scanner class (to obtain user input), Recursion, For loop, While loop, and If – else statements.

For more information about such informative articles, check the trending events page of Leverage Edu.

Leave a Reply

Required fields are marked *

*

*