Welcome back to my blog! In this post, you will learn how to check whether a number is a palindrome or not in Java.
A number is called palindrome if reads same when reversed. For example: 121, 111, 98789, etc.
So let's get started!
Algorithm:
class Palindrome {
public static void main(String[] args) {
int rev, temp, sum = 0;
int n = 121; // The number to be checked whether it is palindrome or not.
temp = n;
while (n > 0) {
r = n%10; // Getting the remainder
sum = (sum * 10) + r;
n = n/10;
}
if(temp == sum)
System.out.println("The number is palindrome!");
else
System.out.println("The number is not palindrome!");
}
}
Output:
The number is palindrome!
Thank you for visiting my blog. Now you have learned how to check whether a number is a palindrome or not in Java.
If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
For more Java tutorials, visit my blog: www.javaoptimist.blogspot.com
A number is called palindrome if reads same when reversed. For example: 121, 111, 98789, etc.
So let's get started!
Algorithm:
- Enter the number which has to be checked.
- Assign the number to a temporary variable.
- Reverse the number.
- Compare the temporary number with the reversed number.
- If both are same, print a message "The number is palindrome!"
- Else print "The number is not palindrome."
class Palindrome {
public static void main(String[] args) {
int rev, temp, sum = 0;
int n = 121; // The number to be checked whether it is palindrome or not.
temp = n;
while (n > 0) {
r = n%10; // Getting the remainder
sum = (sum * 10) + r;
n = n/10;
}
if(temp == sum)
System.out.println("The number is palindrome!");
else
System.out.println("The number is not palindrome!");
}
}
Output:
The number is palindrome!
Thank you for visiting my blog. Now you have learned how to check whether a number is a palindrome or not in Java.
If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
For more Java tutorials, visit my blog: www.javaoptimist.blogspot.com
Comments
Post a Comment