How to Write a Java Program to Check if a Number is a Palindrome
PROGRAMMING
3/18/20243 min read
Introduction
In this blog post, we will explore how to write a Java program to check if a number is a palindrome. We will provide a complete Java code with a detailed explanation of its syntax and usage. Additionally, we will also discuss alternative approaches to create a palindrome program in Java and provide real-life examples where such programs can be used.
Palindrome Number
A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 12321, and 456654 are all palindrome numbers.
Java Program to Check Palindrome Number
Here is the Java code to check if a number is a palindrome:
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber = number;
int reverseNumber = 0;
while (number != 0) {
int remainder = number % 10;
reverseNumber = reverseNumber * 10 + remainder;
number = number / 10;
}
if (originalNumber == reverseNumber) {
System.out.println(originalNumber + " is a palindrome number.");
} else {
System.out.println(originalNumber + " is not a palindrome number.");
}
}
}
Let's break down the code and understand its syntax and usage:
Syntax Explanation
1. Importing Required Classes
The first line of the code imports the Scanner class from the java.util package. The Scanner class allows us to read input from the user.
2. Creating the Main Class
We create a public class named PalindromeNumber, which contains the main method where the program execution begins.
3. Accepting User Input
We create an instance of the Scanner class and prompt the user to enter a number. The entered number is stored in the variable "number".
4. Initializing Variables
We initialize two variables: "originalNumber" to store the original number entered by the user, and "reverseNumber" to store the reversed number.
5. Reversing the Number
We use a while loop to reverse the number. In each iteration, we extract the last digit of the number using the modulo operator (%), add it to the reverseNumber variable, and remove the last digit from the number by dividing it by 10.
6. Checking for Palindrome
After the while loop, we compare the originalNumber with the reverseNumber. If they are equal, we print that the number is a palindrome. Otherwise, we print that the number is not a palindrome.
Alternative Approaches
There are alternative approaches to check if a number is a palindrome in Java. Here are a few:
1. Using String Conversion
One alternative approach is to convert the number to a string and check if the string is equal to its reverse. This can be achieved by using the StringBuilder class to reverse the string.
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
String originalNumber = String.valueOf(number);
String reverseNumber = new StringBuilder(originalNumber).reverse().toString();
if (originalNumber.equals(reverseNumber)) {
System.out.println(originalNumber + " is a palindrome number.");
} else {
System.out.println(originalNumber + " is not a palindrome number.");
}
}
}
2. Using Recursion
Another alternative approach is to use recursion to check if a number is a palindrome. In this approach, we define a recursive function that compares the first and last digits of the number, and then recursively calls itself with the remaining digits.
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
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 number) {
if (number < 10) {
return true;
}
String numberString = String.valueOf(number);
int firstDigit = Integer.parseInt(numberString.substring(0, 1));
int lastDigit = Integer.parseInt(numberString.substring(numberString.length() - 1));
if (firstDigit != lastDigit) {
return false;
}
int remainingNumber = Integer.parseInt(numberString.substring(1, numberString.length() - 1));
return isPalindrome(remainingNumber);
}
}
Real-Life Example
A real-life example where a palindrome program can be used is in the field of data validation. For example, when users enter credit card numbers, it is important to validate if the entered number is a palindrome to ensure its accuracy. This can help prevent errors and fraudulent activities.
By using a palindrome program, developers can quickly check if the entered credit card number is valid or not. This can be done by comparing the entered number with its reversed form. If they are equal, it indicates that the credit card number is a palindrome and hence, valid.
Conclusion
In this blog post, we have learned how to write a Java program to check if a number is a palindrome. We have provided a complete Java code with a detailed explanation of its syntax and usage. Additionally, we have explored alternative approaches to create a palindrome program in Java and discussed a real-life example where such programs can be used. Palindrome programs are useful in various scenarios, including data validation, and understanding their implementation can enhance our programming skills.
Contact
contact@howtodoworld.com