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 and n.
Any number could be Palindrome if it remained the same when we reversed it. For example, 121 is because this remains the same even after reversing it. The subsequent program steps will show the common approach to checking for the Palindrome Number in Java.
- Enter any number
- Reverse the given number
- Compare the original value with the reverse value.
- If they correctly matched, then it is a Palindrome number. Otherwise, it is not.
Java Program to find Palindrome Number Using While Loop
This allows the user to enter any positive number. Next, this program checks whether the given number is a palindrome in Java using While Loop.
// using While loop import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { int Number, t, rem, r = 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 t = Number; while(t > 0) { rem = t %10; r = r * 10 + rem; t = t /10; } System.out.format("Reverse of entered number is = %d\n", r); if(Number == r) { System.out.format("%d is Palindrome.", Number); } else { System.out.format("%d is Not.", Number); } } }
Within this Java palindrome number program, the first two statements, System.out.println statement, will print the statement inside the double Quotes. Next, we assign the user-entered value to an integer variable.
In the next line of this Java palindrome number program, we assigned the original Number value to the Temporary (t) variable. It will help us to preserve our original value and do all the manipulation on the Temporary (t) variable.
Next, we used the While Condition to ensure that the Temporary (t) value is greater than 0 (which Means Positive).
From the above screenshot, the User Entered value: Number = 1441 and r = 0.
t = Number= 1441
First Iteration of the Palindrome Program in Java.
- Finding the Remainder: rem= t % 10 => 1441 % 10 = 1
- Calculating the reverse: r = r * 10 + rem => 0 * 10 + 1 = 1
- Reassigning the Temp value: t = t / 10 => 1441 /10 = 144
Java palindrome number program Second Iteration: From the first iteration, both values changed as t = 144 and r = 1.
- rem = 144 % 10 = 4
- r = 1 * 10 + 4 = 14
- t = t / 10 => 144 /10 = 14
Third Iteration: t = 14 and r = 14.
- rem = 14 % 10 = 4
- r = 14 * 10 + 4 = 144
- t = 14 /10 = 1
Fourth Iteration: From the third Iteration of the Java Palindrome Program, Temp = 1 and Reverse = 144
- rem = 1 % 10 = 1
- r = 144 * 10 + 1 = 1441
- t = 1 /10 = 0
Here, For the next iteration, Temp = 0, so the loop condition will fail.
In the next line, We used the If Condition to check whether the Given Number equals the reverse.
- If the number exactly equals the r, it is a palindrome, and the first System.out.format statement will print. In this example, the Number is 1441, and the reverse is 1441.
- If the number is not equal to the Reversed, it is not. Please refer to the If Else statement article.
Java Program to Check Palindrome Number Using For Loop
This allows the user to enter any positive value. Then, this Java program will check whether the given number is palindrome or not using For Loop.
// using For loop import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { int num, t, r, Reve; sc = new Scanner(System.in); System.out.print("Please Enter any to Check : "); num = sc.nextInt(); t = num; for(Reve = 0; t > 0; t = t/10) { r = t %10; Reve = Reve * 10 + r; } System.out.format("Reverse of entered is = %d\n", Reve); if(num == Reve) { System.out.format("%d is Palindrome.", num); } else { System.out.format("%d is Not.", num); } } }
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, please refer to the For Loop article.
Java Program to find Palindrome Number using String Reverse function
This allows entering any positive integer. Next, this Java program checks whether the given number is palindrome or not using the string buffer reverse function.
// using String Reverse Function 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.print("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); } } }
The following statement in the Java palindrome number program converts the int value to string value and assigns 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 the given value equals the inverse. We used Type Casting to convert the string data to an int value.
Integer.parseInt(rev)
Java Palindrome Number program using Functions
This Java program uses the same steps we followed in our first palindrome number example. However, we separated the logic and placed it in a separate method.
// using Functions 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.print("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; } }
Within this, If you observe the following statement, we call the PaliNum method and assign the return value to int variable rev.
rev = PaliNum(nm);
When the compiler reaches the above line, the compiler will immediately jump to the below function:
public static int PaliNum(int nm) {
We already explained LOGIC in the above example.
Java Program to check Palindrome Number 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 example, we are dividing the code using 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 program, 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 the above example.
NOTE: revNmr (Num/ 10) statement will help to call the function Recursively with the updated value. If you miss this statement, it will terminate after completing the first line.
Main Class Analysis:
Within the Java Palindrome number 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, the method will return an integer value, so we assign that return value to Reverse.
rv = rn.revNm(Number);
In the next line, We used the If Condition to check whether the Given Number is Equal to the inverse of it or not.
Java Program for Palindrome Number between 1 and 1000
This program allows users to enter the minimum and maximum values. This Java program will find and print 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("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
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 program allows entering minimum and maximum values. The Following For Loop helps the compiler to iterate between Minimum and Maximum Variables; iteration starts at the Minimum, and then it will not exceed the Maximum variable.
The if condition checks whether the iteration equals the Reverse number. If it is true, then it is a Palindrome Number, and the format statement is 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 call the evNm function created in our recursive example.
PalNm rn = new PalNm(); rev = rn.revNm(i);