A palindrome number is a number that remains the same when its digits are reversed. In other words, it reads the same backward as forward. For example, 121, 1331, and 454 are palindrome numbers.
Here's a simple Java code to check if a number is a palindrome:
```java
public class PalindromeNumber {
public static void main(String[] args) {
int number = 121; // Change this to any number you want to check
if (isPalindrome(number)) {
System.out.println(number + " is a palindrome number.");
} else {
System.out.println(number + " is not a palindrome number.");
}
}
public static boolean isPalindrome(int num) {
int originalNum = num;
int reversedNum = 0;
while (num > 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
return originalNum == reversedNum;
}
}
```
Description:
1. We define a `Palindrome Number` class and a `main` method to test the palindrome check.
2. The `is Palindrome` method takes an integer `num` and checks if it's a palindrome.
3. Inside the `isPalindrome` method, we reverse the number by extracting its digits one by one and building the reversed number.
4. We compare the original number with the reversed number and return `true` if they are equal, indicating that it's a palindrome, and `false` otherwise.
Conclusion:
The code provided checks if a given number is a palindrome or not. It does so by reversing the digits of the number and comparing the reversed number with the original number. If they are equal, the number is a palindrome. This code can be used to determine if any integer is a palindrome number in Java.
No comments:
Post a Comment