In the following example, we will check whether the given number (121) is a Palindrome number or not.
Example
Java Compiler
public class myClass
{
public static void main(String[] args)
{
int num =121;
int copyNum = num;
int reverse =0;
// reverse a numberwhile(copyNum !=0)
{
reverse = reverse *10;
reverse = reverse + (copyNum %10);
copyNum = copyNum /10;
}
// resultif(num == reverse)
System.out.format("%d is a palindrome number", num);
elseSystem.out.format("%d is not a palindrome number", num);
}
}
Output
121 is a palindrome number
Palindrome Numbers between the Given Range
In the following example, we will find all the Palindrome numbers between 10 and 50.
Example
Java Compiler
public class myClass
{
public static void main(String[] args)
{
int start =10;
int end =50;
int copyNum =0;
int reverse =0;
int flag =0;
System.out.format("Palindrome numbers between %d and %d:\n", start, end);
for(start=start; start<=end; start++)
{
copyNum = start;
reverse =0;
// reverse a numberwhile(copyNum !=0)
{
reverse = reverse *10;
reverse = reverse + (copyNum % 10);
copyNum = copyNum /10;
}
// resultif((start == reverse) && (start !=0))
{
flag =1;
System.out.print(start +" ");
}
}
if(flag ==0)
System.out.print("There is no palindrome number between the given range");
}
}
Output
Palindrome numbers between 10 and 50:
11 22 33 44
Check Whether the Given Number is Palindrome or Not
In the following example, we will check whether the given number is a Palindrome Number or Not.
Example
Java Compiler
import java.util.Scanner;
public class myClass
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
System.out.print("Enter a (int) Number: ");
int num = reader.nextInt();
int copyNum = num;
int reverse =0;
// reverse a numberwhile(copyNum !=0)
{
reverse = reverse *10;
reverse = reverse + (copyNum % 10);
copyNum = copyNum /10;
}
// resultif(num == reverse)
System.out.format("%d is a palindrome number", num);
elseSystem.out.format("%d is not a palindrome number", num);
}
}
Output
Enter a (int) Number: 54
54 is not a palindrome number
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.
We are working to cover every Single Concept in Java.