×

Find whether the given stringnumber is palindrome or not

Problem statement

Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to check whether the given string is palindrome or not. As a programmer, your task is to develop a code that takes a string input and checks whether it is palindrome or not.

Approach

If you know what a palindrome is, you can easily solve this question. A palindrome is a set of sequence of character, which looks similar from either side. It means that if you take a particular sequence and write it from left to right or from right to left, if both the written sequences are the same, then it is a palindrome.

You can solve this by reversing the given string and comparing it with the original string. If both the strings are the same, then you can return true or else false. For this, you need to declare an empty string and traverse the original string from the right and store each character into an empty string. After the completion of string traversal, you need to compare the original string with the new string and return the result.

Example 1.

import java.io.*;
import java.util.*;


public class Main
{
    public static void main (String args [])
    {
        Scanner sc1 = new Scanner (System.in);
        System.out.println("Enter the string to check whether it is palindrome or not:");
        String palindromeString = sc1.next();
// input the string
        String duplicateString = ""; // duplicate string
        int n = palindromeString.length(); // length of the string
        for (int i = n - 1; i >= 0; i--)
        {
            duplicateString = duplicateString + palindromeString.charAt (i); 
           // reversing the string
        }
          // comparing both string
        if (palindromeString.equalsIgnoreCase (duplicateString)
        {
            System.out.println("The given string"+palindromeString+" is palindrome.");
        }
        else
        {
            System.out.println("The string "+palindromeString+"is not palindrome.");
        }
    }
}

Output

Find whether the given string/number is palindrome or not
Find whether the given string/number is palindrome or not

Example 2: If input is in the number form.

import java.io.*;
import java.util.*;


class Main {
  public static void main (String [] args) {
      Scanner sc1= new Scanner (System.in);
      System.out.println("enter the number to check whether it is palindrome or not");
      int Palindromenumber = sc1.nextInt(); // input the integer 
      int revNum = 0, remainder = 0;
    
      // storing the input number to orgNum
      int orgNum = Palindromenumber;
    
      // getting thr reverse of orgNum
      // storing it to revNum
      while (Palindromenumber != 0) {
        remainder = Palindromenumber % 10 ; 
        revNum = revNum * 10 + remainder ;
        Palindromenumber /= 10 ;
      }
    
      // check if revNum and orgNumNum are equal
      if (orgNum == revNum) {
        System.out.println(orgNum + " is a Palindrome.");
      }
      else {
        System.out.println(orgNum + " is not a Palindrome.");
      }
  }
}

Output

Find whether the given string/number is palindrome or not
Find whether the given string/number is palindrome or not

Related Topics

Compound Interest GUI Calculator using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

Python Bubble Sort

Bubble sort is one of the techniques used to sort the elements in lists in a certain order, either in ascending or descending order. It is called Bubble sort because...

4 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Index Error in Python

The index errors are the run time error that is raised in Python when we try to access an index that does not exist. This might seem very trivial but...

4 minutes read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

Python Set add() Method

Python Set add() Method The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it. Syntax set.add(element) Parameter element- This parameter represents the element that...

1 minute read.

Python String Lowercase

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.