Palindrome Number in Java

In this tutorial, we will cover what is palindrome number in java and palindrome program. In most of the testing interviews, question asked on palindrome program in java so you should have knowledge about this.

What is palindrome Number?

Palindrome number is a number which is same as reverse. For example 151, 343, 545, 34543, 99099 are the palindrome numbers. In string it is MADAM, ABBA, LOL etc.

Palindrome Number Algorithm

Put the number to check for palindrome

Store the number in temporary variable

Reverse this number

Compare this temporary number with reversed number

If  numbers are same, print “It is a palindrome no.” Else print “It is not a palindrome no.”

Let’s see the palindrome program in Java.

 

Palindrome number in Java using while loop

class PalindromeNo{
public static void main(String args[])
{ 
  int rr,sum=0,temp; int no=454;
  //Will check this number variable for palindrome temp=no; 
  while(no>0)
  { 
    rr=no%10; 
  //getting remainder 
   sum=(sum*10)+r; n=n/10; 
 }
  if(temp==sum)
   System.out.println("This is a palindrome number "); 
   else
   System.out.println("This is not a palindrome number");
 } }

 

Output:

This is a palindrome number 

Watch this video: –

https://www.youtube.com/watch?v=kbvt6Ikk8no&ab_channel=SmartProgramming

Palindrome number in java using for loop

public class PalindromeNo {

    public static void main(String[] args) {

        int num = 1122, reverseInteger = 0, remainder, origInteger;

        origInteger = num;

        // reverse integer is stored in variable
        for( ;num != 0; num /= 10 )
        {
            remainder = num % 10;
            reverseInteger = reverseInteger * 10 + remainder;
        }

        
        if (origInteger == reverseInteger)
            System.out.println(origInteger + " is a palindrome.");
        else
            System.out.println(origInteger + " is not a palindrome.");
    }
}

Output:
1122 is not a palindrome.

In the above program, for loop is used instead of a while loop.

In above program, on each iteration, num /= 10 is executed and condition num !=0 is checked.


String Palindrome program in java using reverse method

Palindrome String
                                                                              String Palindrome
	class PalindromeEx2  
	{  
	   public static void main(String args[])  
	   {  
	      String orig, rev = ""; // Objects of String class  
	      Scanner in = new Scanner(System.in); 
  
	      System.out.println("Enter a string or number to check if it is a palindrome or not"); 
 
	      orig = in.nextLine();   
	      int length = orig.length();   
	      for ( int i = length - 1; i >= 0; i-- ) 
 
	         rev = rev + orig.charAt(i);
  
	      if (orig.equals(rev)) 
 	         System.out.println("This is a palindrome.");
  
	      else  
	         System.out.println("This isn't a palindrome.");   
	   }  
	}  



Palindrome numbers between 1 to 100 in java

In this program, we will find the palindrome numbers in given range. By using for loop we will perform this.

public class PalindromesEx {

 public static void main(String[] args) {
  int min = 0;
  int max = 100;

  printPal(min, max);
 }

private static boolean isPalindrome(int no) {

  // Reversing a number
  int reverse = 0;
  for (int i = num; i > 0; i /= 10)
   reverse = reverse * 10 + i % 10;

  // If no and reverse are same, then no is palindrome
  return
  no == reverse;
 }

 
 static void printPal(int min, int max)
 {
  for (int i = min; i <= max; i++)

   if (isPalindrome(i))

    System.out.print(i + " ");
 }
}

 

Summary

Hope this tutorial is helpful for your automation testing interviews.

Final words- Bookmark this post “Palindrome Number in Java” for future reference.

In future I will add more Programs in Java.

Must read this Post:

Armstrong Number In Java

Leave a Comment