Palindrome program in Java – Here, we will discuss the various methods to check whether a given number is a palindrome or not. A compiler is added so that you can execute the program yourself, alongside various examples and sample outputs are given. The methods aforementioned are:
- Print using For Loop
- Print using Static Method
- Print using Recursion
- Print using String
A Palindrome is a sequence of words, digits or other symbols that reads the same both forwards and backwards.
There are several examples such as RACECAR, MOM, 12321 etc.
This is another example of a palindrome:
The speciality of this palindrome is that this is the longest palindrome in the existence of the English Language. This palindrome was termed by James Joyce in Ulysses in 1922.
The meaning of this word is simply a knock at the door.
Thus, the methods with which the string is a palindrome or not be determined in Java programs are as follows:
Using For Loop
1) The number is called a palindrome if a number is equal to reverse of its number.
2) For loop repeats rem=num%10;, s=(s*10)+rem these steps until num>0. If the condition is false, then it compares the s value with t, if both are equal then it prints the given number is a palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class Palindrome { public static void main(String arg[]) { int num,t,s,rem; Scanner sc=new Scanner(System.in); System.out.println("Enter any number "); num=sc.nextInt(); t=num; for(s=0;num>0;num/=10) { rem=num%10; s=(s*10)+rem; } if(s==t) System.out.println(t+" is a palindrome number "); else System.out.println(t+" is not a palindrome number "); } } |
Output:
1 2 3 |
Enter any number 121 121 is a palindrome number |
1 2 3 |
Enter any number 12 12 is not a palindrome number |
Using Static Method
1) In this program we have the static method palindromeOrNot(int num ), it calculates the reverse of the given number.
2) Call the palindromeOrNot(int num ) method in the main method. In this method while loop repeats r=num%10; sum=(sum*10)+r; num/=10; these steps until num!=0 is false. If num=0 then it returns the sum, then compare the original number with this reverse number, if both are equal then it prints given number is a palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class Pd { public static void main(String arg[]) { int n,t,s; Scanner sc=new Scanner(System.in); System.out.println("Enter any number "); n=sc.nextInt(); t=n; s=palindromeOrNot(n); if(s==t) System.out.println(t+" is a palindrome number "); else System.out.println(t+" is not a palindrome number "); } static int palindromeOrNot(int num ) { int sum=0,r; while(num!=0) { r=num%10; sum=(sum*10)+r; num/=10; } return sum; } } |
Output:
1 2 3 |
Enter any number 135 135 is not a palindrome number |
Using Recursion
1) Using the “Palin” object p, we will call the palindromeOrNot(a) method.
2) The method palindromeOrNot(int num) calls itself as palindromeOrNot(num) until num!=0, if num=0 then it returns sum and sum assigned to s and compares with t, if both are equal then it prints message as “palindrome number”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Scanner; class Palin { int sum=0,r; int palindromeOrNot(int num) { if(num!=0) { r=num%10; sum=(sum*10)+r; num/=10; palindromeOrNot(num); } return sum; } public static void main(String arg[]) { int a,t,s; Palin p=new Palin(); Scanner sc=new Scanner(System.in); System.out.print("Enter a number :"); a=sc.nextInt(); t=a; s=p.palindromeOrNot(a); if(s==t) System.out.println("Palindrome number "); else System.out.println("Not a Palindrome number "); } } |
Output:
1 2 |
Enter a number :123454321 Palindrome number |
Using String
1) Here we are comparing the two strings.
2) Entered string will be assigned to str variable, here str is string datatype.
3) For loop iterates until i>=0, here i=length of the string -1. length() is the string class method.
Example str=Dad so string length=3,
i=length-1=2 then strrev=””+d =d (here charAt(2) i.e character at the index 2 is d)
i=1 then strrev=d+a(here charAt(1) i.e character at the index 1 is a)
i=0 then strrev=da+D(here charAt(0) i.e character at the index 0 is D)
4) After all iterations srtrev=daD, now compare the two strings using strrev.equalsIgnoreCase(str), this is string class method, it compares the characters available in strrev string with str. If both the characters are equal then prints entered string is palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Scanner; class Palindrome { public static void main(String arg[]) { String str,strrev=""; Scanner sc=new Scanner(System.in); System.out.print("Enter a string :"); str=sc.next(); for(int i=str.length()-1;i>=0;i--) { strrev=strrev+str.charAt(i); } System.out.println("Reverse of a string is : "+strrev); if(strrev.equalsIgnoreCase(str)) System.out.println("Entered string is palindrome"); else System.out.println("Entered string is not a palindrome number "); } } |
Output:
1 2 3 |
Enter a string : Dad Reverse of a string is : daD Entered string is palindrome |
1 2 3 |
Enter a string : Palindrome Reverse of a string is : emordnilaP Entered string is not a palindrome number |