Palindrome Program in Java: Number is Palindrome or Not

Published December 23, 2020

What is a Palindrome Number?

A palindromic number is actually just a number that remains exactly the same when its digits are reversed. Quite simply, it's reflectional symmetry around a perpendicular axis. The expression palindromic is based on a palindrome, which describes to some note whose text is more unchanged if its own characters have been now reversed.

 

Algorithm for String Palindrome Program in Java

  • Take input from a user and store it in any variable.

  • Now copy the same number and store it in the temporary variable.

  • Reverse the temporary variable.

  • Compare the value of the temporary variable with the original string or number.

  • If they both are the same then print that the number is a palindrome.

  • If not then print that the number is not a palindrome.

 

Program to check whether the String is Palindrome or Not

import java.util.Scanner;
class palindromeString

{
  public static void main(String args[])

  {
      String str, rev = "";

     Scanner sc = new Scanner(System.in);

     System.out.println("Enter a string:");

     str = sc.nextLine();

     int length = str.length();

     for ( int i = length - 1; i >= 0; i-- )

        rev = rev + str.charAt(i);

     if (str.equals(rev))
        System.out.println(str+" is a palindrome");

     else
        System.out.println(str+" is not a palindrome");
   }

}

 

Output:

Enter a string:

NITIN

NITIN is a palindrome

 

Program to check whether the Input Number is Palindrome or Not

public class PalindromeNumber {

    public static void main(String[] args)

    {
        int lastDigit,sum=0,a;

        int originalNumber=252; //The number you want to check whether it is palindrome or not
       
        a=originalNumber;
// Code to reverse a number

        while(a>0)

        { System.out.println("Original Number "+a);

              lastDigit=a%10; //get remainder

              System.out.println("Last Digit "+lastDigit);

              System.out.println("Digit "+lastDigit+ "  added in sum "+(sum*10));

              sum=(sum*10)+lastDigit;

              a=a/10;
}

// if given number equal to sum than number is palindrome otherwise not palindrome

            if(sum==originalNumber)

                System.out.println("Number is palindrome ");

            else

                System.out.println("Number is not palindrome");

}

}

 

Output

Original Number: 252

Last digit: 2

Digit 2 added in sum 0

Original Number: 25

Last digit: 5

Digit 2 added in sum 20

Original Number: 2

Last digit: 2

Digit 2 added in sum 250

Number is Palindrome

 

Palindrome In Java

 

If you are a Java beginner developer you want to start to create Java project then here are the  15 Java Projects for Beginners

 

Tags: palindrome program in java, palindrome string program in java, string palindrome program in java, palindrome number program in java

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

581 Views