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 a palindrome number because this number remains the same even after reversing it.

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

  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. Otherwise, it is not the Java Palindrome number.

Palindrome Program in Java Using While Loop

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

// Palindrome Program in Java Using While Loop

package SimplerPrograms;

import java.util.Scanner;

public class PalindromeNumberusingWhile {
	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 for Palindrome: ");
		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 a Palindrome Number.", Number);
		}
	}
}
Palindrome Program in Java Using While Loop

Within this Palindrome in Java, the following 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 (Number)

System.out.println("Please Enter any number to Check for Palindrome: ");
Number = sc.nextInt();

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.

Temp = Number;

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

while(Temp > 0) {
	Reminder = Temp %10;
	Reverse = Reverse * 10 + Reminder;
	Temp = Temp /10;
}

From the above palindrome program 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 = Reverse * 10 + Reminder
Reverse = 0 * 10 + 1
Reverse= 1

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

Second Iteration
From the first Iteration, the values of both the Temp and Reverse changed as Temp = 144 and Reverse = 1

Finding the Remainder
Reminder = Temp % 10 => 144 % 10 = 4

Calculating the Reverse
Reverse = Reverse * 10 + Reminder => 1 * 10 + 4
Reverse = 14

Reassigning the Temp value
Temp = Temp / 10 => 144 /10
Temp = 14

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

Reminder = Temp % 10 => 14 % 10 = 4

Reverse = Reverse * 10 + Reminder => 14 * 10 + 4
Reverse = 144

Temp = Temp / 10 => 14 /10
Temp = 1

Fourth Iteration
From the third Iteration, Temp = 1 and Reverse = 144

Reminder = Temp % 10
Reminder = 1 % 10 = 1

Reverse = Reverse * 10 + Reminder => 144 * 10 + 1
Reverse = 1441

Temp = Temp / 10 => 1 /10
Temp = 0

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

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

  1. If the Number is exactly equal to the Reversed number, then the number is a palindrome and System.out.format(“%d is Palindrome Number.”, Number); statement will be printed. In this example, the Number is 1441, and the reversed number is also 1441, so palindrome.
  2. If the number is not equal to the Reversed number, then the number is a palindrome and System.out.format(“%d is Not a Palindrome Number.”, Number); statement will be printed. Please refer Java If Else statement article to understand the If else statement.

Palindrome Program in Java Using For Loop

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

// Palindrome Program in Java Using For Loop

package SimplerPrograms;

import java.util.Scanner;

public class PalindromeNumberusingFor {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number, Temp, Reminder, Reverse;
		sc = new Scanner(System.in);
		
		System.out.println("Please Enter any number to Check for Palindrome: ");
		Number = sc.nextInt();
		Temp = Number;
		for(Reverse = 0; Temp > 0; Temp = Temp/10) {
			Reminder = Temp %10;
			Reverse = Reverse * 10 + Reminder;
		}
		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 a Palindrome Number.", Number);
		}
	}
}
Please Enter any number to Check for Palindrome: 
1896981
Reverse of entered number is = 1896981
1896981 is Palindrome Number.

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

Java Palindrome Program using String Reverse

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

package SimplerPrograms;

import java.util.Scanner;

public class PalindromeNumberusingReverse {
	private static Scanner sc;

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

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

String str = Integer.toString(Number);

Next, we used the String Buffer reverse function to reverse the string.

String reverse = new StringBuffer(str).reverse().toString();

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

Integer.parseInt(reverse)

Palindrome Program in Java using Functions

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

// Palindrome Program in Java using Functions
package SimplerPrograms;

import java.util.Scanner;

public class PalindromeNumberusingFunctions {
 private static Scanner sc;
 private static int Reverse = 0;
 public static void main(String[] args) {
 int Number;
 sc = new Scanner(System.in);
 
 System.out.println("Please Enter any number to Check for Palindrome: ");
 Number = sc.nextInt();
 
 Reverse = PalindromeNumber(Number);
 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 a Palindrome Number.", Number);
 }
 }
 public static int PalindromeNumber(int Number) {
 int Reminder;
 while(Number > 0) {
 Reminder = Number % 10;
 Reverse = Reverse * 10 + Reminder;
 Number = Number /10;
 }
 return Reverse;
 }
}
Please Enter any number to Check for Palindrome: 
1221
Reverse of entered number is = 1221
1221 is Palindrome Number.

Within this palindrome program in Java, If you observe the following statement, we are calling the PalindromeNumber(Number) method and assigning the return value to integer variable Reverse.

Reverse = PalindromeNumber(Number);

When the compiler reaches to PalindromeNumber(Number) line in main() Java Palindrome program then the compiler will immediately jump to below function:

public static int PalindromeNumber(int Number) {

We already explained LOGIC in the above Java Palindrome number example.

Palindrome Program in Java using Recursion

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

In this Java Palindrome program 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 reverse an integer recursively.

package SimplerPrograms;

public class PalindromeNumber {
	int Reverse = 0;
	public int reverseNumber(int Number) {
		int Reminder;
		if(Number > 0) {
			Reminder = Number %10; 
			Reverse = Reverse * 10+ Reminder;
			reverseNumber(Number /10);
		}
		return Reverse;
	}
}

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

package FrequentPrograms;

import java.util.Scanner;

import SimplerPrograms.PalindromeNumber;

public class PalindromeNumberusingRecursion {
 private static Scanner sc;

 public static void main(String[] args) {
 int Number, Reverse = 0;
 sc = new Scanner(System.in);
 
 System.out.println("Please Enter any number to Check for Palindrome: ");
 Number = sc.nextInt();
 
 PalindromeNumber rn = new PalindromeNumber();
 Reverse = rn.reverseNumber(Number);
 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 a Palindrome Number.", Number);
 }
 }
}
Please Enter any number to Check for Palindrome: 
1991
Reverse of entered number is = 1991
1991 is Palindrome Number.

PalindromeNumber Class Analysis:

In this Java palindrome program, we declared an integer function reverseNumber with one argument. Within the palindrome 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: reverseNumber (Number / 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 PalindromeNumber Class

PalindromeNumber rn = new PalindromeNumber();

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

Reverse = rn.reverseNumber(Number);

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

Java Program for Palindrome Number between 1 and 1000

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

package SimplerPrograms;

import java.util.Scanner;

public class PalindromeNumbersuptoMax {
 private static Scanner sc;
 public static void main(String[] args) {
 int Number, Reverse, 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("Palindrome numbers Between %d and %d are:\n",Minimum, Maximum);
 for(Number = Minimum; Number <= Maximum; Number++) {
 Temp = Number;
 for(Reverse = 0; Temp > 0; Temp = Temp/10) {
 Reminder = Temp %10;
 Reverse = Reverse * 10 + Reminder;
 }
 if(Number == Reverse) {
 System.out.format("%d ", Number);
 }
 }
 }
}

Java Palindrome Number from 1 to 1000 output

Please Enter the Minimum Value: 
1
Please Enter the Maximum Value: 
1000
Palindrome 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 program allows to enter minimum and maximum values. 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.

for(Number = Minimum; Number <= Maximum; Number++) {

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

if(Number == Reverse) {
	System.out.format("%d ", Number);
}

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

for(Reverse = 0; Temp > 0; Temp = Temp/10) {
	Reminder = Temp %10;
	Reverse = Reverse * 10 + Reminder;
}

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

PalindromeNumber rn = new PalindromeNumber();
Reverse = rn.reverseNumber(Number);