Palindrome Program in Java

Write a Palindrome Program in Java using While Loop, For Loop, Built-in reverse function, Functions, and Recursion. We will also show the Java program to print Palindrome Numbers between 1 to n.

Any number could be Palindrome in Java if it remained the same when we reversed it. For example, 121 is because this remains the same even after reversing it.

The subsequent steps will show the common approach to check for the Palindrome Number in Java

  1. Enter any number
  2. Reverse the given number
  3. Compare the original value with reverse value.
  4. If they correctly matched, then it is a Palindrome number in java. Otherwise, it is not.

Palindrome Program in Java Using While Loop

This allows the user to enter any positive number. Next, this program checks whether the given number is palindrome number in Java using While Loop.

package SimplerPrograms;

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number, Temp, Reminder, Reverse = 0;
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter any number to Check : ");
		Number = sc.nextInt();
		//Helps to prevent altering the original value
		Temp = Number;
		while(Temp > 0) {
			Reminder = Temp %10;
			Reverse = Reverse * 10 + Reminder;
			Temp = Temp /10;
		}
		System.out.format("Reverse of entered number is = %d\n", Reverse);
		if(Number == Reverse) {
			System.out.format("%d is Palindrome Number.", Number);
		}
		else  {
			System.out.format("%d is Not.", Number);
		}
	}
}
Palindrome Program in Java Using While Loop

Java Palindrome Number program explanation

Within this program, the first two statements, System.out.println statement will print the statement inside the double Quotes. And next we are assigning the user entered value to integer variable.

Next line of this Java palindrome number program, we assigned the original Number value to the Temp variable. It will help us to preserve our original value and do all the manipulation on the Temp variable.

Next, we used the While, and the Condition inside it will make sure that the Temp value is greater than 0 (Means Positive).

From the above screenshot, User Entered value: Number = 1441 and Reverse = 0
Temp = Number= 1441

First Iteration
Finding the Remainder
Reminder= Temp % 10
Reminder= 1441 % 10 = 1

Calculating the
Reverse = Reverse * 10 + Reminder => 0 * 10 + 1 = 1

Reassigning the Temp value
Temp= Temp / 10 = 1441 /10
Temp= 144

Java palindrome number program Second Iteration: From the first Iteration, the values of both changed as Temp = 144 and Reverse = 1

Reminder = 144 % 10 = 4

Reverse = 1 * 10 + 4 = 14

Temp = Temp / 10 => 144 /10
Temp = 14

Third Iteration: From the Second Iteration of Java Palindrome number program, Temp = 14 and Reverse = 14

Reminder = 14 % 10 = 4

Reverse = 14 * 10 + 4 = 144

Temp = 14 /10 = 1

Java palindrome number Fourth Iteration: From the third Iteration, Temp = 1 and Reverse = 144

Reminder = 1 % 10 = 1

Reverse = 144 * 10 + 1 = 1441

Temp = 1 /10 = 0

Here, For the next iteration Temp = 0 so, the loop condition will fail

In the next line of palindrome program in java, We used If Condition to check whether the Given Number is Equals to Reverse or not.

  1. If the Number is exactly equals to the Rev number, then the number is a palindrome and the first System.out.format statement will be printed. In this example, the Number= 1441, and the reversed is also 1441.
  2. If the number is not equal to the Reversed, then it is not. Please refer If Else statement article to understand the If else statement.

Palindrome Program in Java Using For Loop

This allows the user to enter any positive number. Then this Java program will check whether the given number is palindrome or not using For Loop

import java.util.Scanner;
public class Example {		
	private static Scanner sc;	
	public static void main(String[] args) 
	{	
		int num, Temp, Reminder, Reve;
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter any to Check : ");
		num = sc.nextInt();
		Temp = num;
		for(Reve = 0; Temp > 0; Temp = Temp/10) {
			Reminder = Temp %10;
			Reve = Reve * 10 + Reminder;
		}
		System.out.format("Rev of entered is = %d\n", Reve);
		if(num == Reve) {
			System.out.format("%d is Palindrome.", num);
		}
		else  {
			System.out.format("%d is Not.", num);
		}
	}
}
Please Enter any to Check : 
1896981
Rev of entered is = 1896981
1896981 is Palindrome.

We have not done anything special in this example. We just replaced the While with For Loop in the above code. If you find it challenging to understand the For loop functionality, then please refer to For Loop article.

Java Palindrome Program using String Reverse function

This allows entering any positive integer. Next, this Java program checks the given number is palindrome or not using the string buffer reverse function.

package SimplerPrograms;

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		int Num;
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter any value to Check : ");
		Num = sc.nextInt();
		
		String str = Integer.toString(Num);
		String rev = new StringBuffer(str).reverse().toString();
		System.out.format("Rev = %s \n", rev);
		if(Num == Integer.parseInt(rev)) {
			System.out.format("%d is Palindrome.", Num);
		}
		else  {
			System.out.format("%d is Not.", Num);
		}
	}
}
Please Enter any value to Check : 
121
Rev = 121 
121 is Palindrome.

The following statement in the Java palindrome program converts the int value to string value and also assign the converted value to the Str variable.

String str = Integer.toString(Num);

Next, we used the String Buffer library method to reverse the given string.

Lastly, we used the If statement to check whether Given Number is Equal to the inverse or not. Here we used the Type Casting to convert the string data to an int value

Integer.parseInt(rev)

Palindrome in Java using Functions

In this Java program, we are using the same steps that we followed in our first java palindrome number example. However, we separated the logic and placed it in a separate method.

import java.util.Scanner;

public class Example {	
	
	private static Scanner sc;	
	 private static int rev = 0;
	 
	public static void main(String[] args) 
	{	
		 int nm;
		 sc = new Scanner(System.in);
		 
		 System.out.println("Please Enter any to Check : ");
		 nm = sc.nextInt();
		 
		 rev = PaliNum(nm);
		 System.out.format("Inverse of entered is = %d\n", rev);
		 
		 if(nm == rev) 
		 {
			 System.out.format("%d is Palindrome.", nm);
		 }
		 else  
		 {
			 System.out.format("%d is Not.", nm);
		 }
	}
	
	public static int PaliNum(int nm) 
	{
		 int rem;
		 while(nm > 0) 
		 {
			 rem = nm % 10;
			 rev = rev * 10 + rem;
			 nm = nm /10;
		 }
		 return rev;
	}
}
Please Enter any to Check : 
1221
Inverse of entered is = 1221
1221 is Palindrome.

Within this, If you observe the following statement, we are calling the PaliNum method and assigning the return value to int variable rev.

rev = PaliNum(nm);

When the compiler reaches to the above line in this Java Palindrome number program then the compiler will immediately jump to below function:

public static int PaliNum(int nm) {

We already explained LOGIC in the above example.

Palindrome Number Program in Java using Recursion

It helps us to enter any positive number. Then this Java program will check whether the given number is palindrome or not using the Recursion.

In this Java Palindrome example, we are dividing the code using the Object-Oriented Programming. To do this, First, we will create a class that holds a method to inverse the number recursively.

package SimplerPrograms;

public class PalNm {
	int rv = 0;
	public int revNm(int Num) {
		int Reminder;
		if(Num > 0) {
			Reminder = Num %10; 
			rv = rv * 10+ Reminder;
			revNm(Num /10);
		}
		return rv;
	}
}

Within the Main palindrome in Java, we will create an instance of the above-specified class and call the methods

package FrequentPrograms;

import java.util.Scanner;

import SimplerPrograms.PalNm;

public class Example {
 private static Scanner sc;

 public static void main(String[] args) {
 int n, rv = 0;
 sc = new Scanner(System.in);
 
 System.out.println("Please Enter any number : ");
 n = sc.nextInt();
 
 PalNm rn = new PalNm();
 rv = rn.revNm(n);
 System.out.format("Reversed Number is = %d\n", rv);
 if(Number == rv) {
 System.out.format("%d is Palindrome.", n);
 }
 else  {
 System.out.format("%d is Not.", n);
 }
 }
}
Please Enter any : 
1991
Reversed Number is = 1991
1991 is Palindrome.

Class Analysis:

In this Java palindrome number program example, we declared a function revNum with one argument. Within the function, we used the If statement to check whether the given number is greater than Zero or Not and if it is True then statements inside the If block will be executed. We already explained the Logic in above example.

NOTE: revNmr (Num/ 10) statement will help to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it will terminate.

Main Class Analysis:

Within the Java palindrome program main class, First we created an instance / created an Object of the PalNm Class

PalNm rn = new PalNm();

Next, we are calling the revNm method. As we all know that the method will return integer value so, we are assigning that return value to Reverse

rv = rn.revNm(Number);

In the next line, We used If Condition to check whether the Given Number is Equal to inverse of it or not.

Java Program for Palindrome Number between 1 and 1000

This Java program allows users to enter the minimum and maximum values. This program will find the Palindrome Number between the Minimum and Maximum values.

import java.util.Scanner;

public class Example {	
	
	private static Scanner sc;	
	 
	public static void main(String[] args) 
	{	
		 int i, rev, Reminder = 0, Temp, Minimum, Maximum;
		 sc = new Scanner(System.in);
		 
		 System.out.println("Please Enter the Minimum Value: ");
		 Minimum = sc.nextInt(); 
		 
		 System.out.println("Please Enter the Maximum Value: ");
		 Maximum = sc.nextInt();
		 
		 System.out.format("Numbers Between %d and %d are:\n",Minimum, Maximum);
		 for(i = Minimum; i <= Maximum; i++) 
		 {
			 Temp = i;
			 for(rev = 0; Temp > 0; Temp = Temp/10) 
			 {
				 Reminder = Temp %10;
				 rev = rev * 10 + Reminder;
			 }
			 if(i == rev) 
			 {
				 System.out.format("%d ", i);
			 }
		 }
	}
}
Please Enter the Minimum Value: 
1
Please Enter the Maximum Value: 
1000
Numbers Between 1 and 1000 are:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999 

This Java palindrome number program allows to enter minimum and maximum values. The Following For Loop helps compiler to iterate between Minimum and Maximum Variables, iteration starts at the Minimum and then it will not exceed Maximum variable.

The if condition checks whether the iteration is exactly equal to the Reverse number or not. If it is true, then it is Palindrome Number, and the format statement printed.

To show everything in one place, we placed the Nested For loop

Remember, you can replace the above code snippet with the following code. Here, we are calling the evNm function that we created in our recursive example.

PalNm rn = new PalNm();
rev = rn.revNm(i);