check whether a number is palindrome or not – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 13 Aug 2022 16:46:01 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg check whether a number is palindrome or not – Programmerbay https://programmerbay.com 32 32 Palindrome Program in Java https://programmerbay.com/palindrome-program-in-java/ https://programmerbay.com/palindrome-program-in-java/#respond Tue, 02 Aug 2022 06:48:49 +0000 https://www.programmerbay.com/?p=2379 In this article, we’ll be implementing palindrome Java programs for both strings and numbers. It is the most commonly asked question in the technical round for freshers.

A palindrome is a number, string or sequence that spells the same if we read it either from backwards or forward. For example, bob, if one reads it from, either way, it’ll be pronounced the same.

Palindrome Number Program in Java

The Java program checks whether a number or string is a palindrome or not.  A palindrome number can be defined as a number or string that is equivalent to its reversed form. For example, 101 is the same as its reverse but 102 isn’t because its reverse would give you 201.

irect

There are two approaches to achieve this functionality :

  1. Using loop (either for loop or while loop)
  2. Using recursion

Palindrome Number Program Algorithm 

  • Accept input from the user
  • Reverse the provided input number
  • Compare the reversed input number with the actual one provided by the user
  • If both are same then it is a palindrome number
  • Otherwise, it not a palindrome

Example 1. Java Program to Check Palindrome Number Using Loop

Program:

/*
*
Palindrome Number Program in Java
*
*/

import java.util.Scanner;
public class PalindromeProgram {
public static void main(String[] args) {
int number,remainder,temp,i,sum=0;
System.out.print("\n Please enter a number = ");
Scanner ed = new Scanner (System.in);
number = ed.nextInt();
temp=number;
while(temp!=0)
{
remainder=temp%10;
sum=sum*10+remainder;
temp=temp/10;
}
if(sum==number)
System.out.println("It is a palindrome");
else
System.out.println("It is not a palindrome");
}

}

Output:

Please enter a number = 11311
It is a palindrome

Code Explanation:

1) Getting input from the user
2) Storing it to a temporary variable ‘temp’
3) While temp is not equal to zero, do
4) Find the last digit from input by using the modulo operator which would give remainder, for example, if we divide 1001 by 10, we would get 1 as remainder. In simple words, this how we are obtaining the last digit on each iteration

5) Adding the result with ‘sum'(initially 0 ) variable which is multiplied first with 10 on each iteration (Creating the reverse of that input)
6) Dividing ‘temp’ by 10 and also assigning it to ‘temp’
7) Repeat 3 to 6 till ‘temp’ is not equal to 0
8) Checking whether the final sum stored in ‘sum’ variable is equivalent to the input or not
9) If yes then it is a palindrome number

Example 2. Java Program to Check Palindrome Number using Recursion

Program: 

public class PalindromeNumber {
    public static void main(String[] args) {
        int number = 1221;
        int reversedNum =0;
        reversedNum =  getReversedNumber(number,reversedNum);

        if(reversedNum == number){
            System.out.println("It's a palindrome number");
        }else{
            System.out.println("It's not a palindrome number");
        }

    }

    public static int getReversedNumber(int num, int rev){
        int remainder;
        if(num == 0){
            return  rev;
        }
        remainder = num % 10;
        rev = rev*10 + remainder;
        num = num / 10;
        return getReversedNumber(num,rev);
    }
}

Output:

It's a palindrome number

Code Explanation:

In the above code, we have followed the same approach as first example, but in this case, we are using recursion method to achieve the same. A recursion function calls itself until it reaches to certain condition.

Palindrome String Program in Java

A string is said to be palindrome string that gives the same string if we read it from the back. For Example, Mom if we reverse it we still get the same string.

irect 1

There are two ways to check for string palindrome.

  1. Using Loop
  2. Using StringBuilder class

Example 1. Java Program to Check Palindrome String using Loop

import java.util.Scanner;

public class StringPalindrome {
    public static void main(String[] args) {
        String word, reversed = "";
        char ch[];
        int i, size;
        System.out.print("Please enter a String = ");
        Scanner ed = new Scanner(System.in);
        word = ed.nextLine();
        ch = word.toCharArray();
        size = word.length() - 1;
        for (i = size; i >= 0; i--) {
            reversed = reversed + ch[i];
        }
        if (word.trim().equalsIgnoreCase(reversed))
            System.out.println(word + " is a string palindrome as it reverse is same as = " + reversed);
        else System.out.println(word + " is not a string palindrome as it reverse isn't same as = " + reversed);
    }
}

StringBuilder class provides us reverse() method which can be used to make things simpler and easier.

Example 2. Java Program to Check Palindrome String using StringBuilder

program:

import java.util.Scanner;

public class StringPalindrome {
    public static void main(String[] args) {
        String word, reversed = "";
        System.out.print("Please enter a String = ");
        Scanner ed = new Scanner(System.in);
        word = ed.nextLine();
        StringBuilder string = new StringBuilder(word);
        reversed = string.reverse().toString();
        if (word.trim().equalsIgnoreCase(reversed))
            System.out.println(word + " is a string palindrome as it reverse is same as = " + reversed);
        else System.out.println(word + " is not a string palindrome as it reverse isn't same as = " + reversed);
    }

Output:

Please enter a String = mom 
mom is a string palindrome as it reverse is same as = mom

Example 3. Java Program to Check Palindrome String using recursion

Program : 

public class PalindromeString {
    public static void main(String[] args) {
        String str = "mom";
        boolean isPalndromeStr =  isPalindrome(str.toLowerCase(),0,str.length()-1);

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

    }

    public static boolean isPalindrome(String str, int forwrdIndx, int backwrdIndx){
        if(forwrdIndx > backwrdIndx){
            return  true;
        }
        if(str.charAt(forwrdIndx) != str.charAt(backwrdIndx)){
            return  false;
        }
        return isPalindrome(str,++forwrdIndx,--backwrdIndx);
    }
}

Output:

mom is a palindrome string

]]>
https://programmerbay.com/palindrome-program-in-java/feed/ 0