Table of Contents
Java program to check if a number is a palindrome:- A palindrome is a number (or a word, phrase, or sequence) that reads the same forward and backward. For example, 121, 1331, and 12321 are palindrome numbers. Checking if a number is a palindrome is a common programming exercise that involves reversing the number and comparing it with the original.
Java Program to Check if a Number is a Palindrome
Here’s a Java program that prompts the user to enter a number and then checks if that number is a palindrome.
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check if the number is a palindrome
boolean isPalindrome = isPalindrome(number);
// Print the result
if (isPalindrome) {
System.out.println(number + " is a palindrome.");
} else {
System.out.println(number + " is not a palindrome.");
}
}
// Method to check if a number is a palindrome
public static boolean isPalindrome(int n) {
int originalNumber = n;
int reversedNumber = 0;
// Reverse the number
while (n != 0) {
int digit = n % 10;
reversedNumber = reversedNumber * 10 + digit;
n /= 10;
}
// Compare the original number with the reversed number
return originalNumber == reversedNumber;
}
}
Explanation of the Code: (Java program to check if a number is a palindrome)
Let’s break down the program step-by-step to understand how it works.
- Importing the Scanner Class: We import the
Scanner
class from thejava.util
package to read input from the user.
import java.util.Scanner;
2. Creating the Main Class: We create a public class named PalindromeCheck
. In Java, the class name should match the filename.
public class PalindromeCheck {
3. Main Method: The main
method is the entry point of the program. Inside this method, we will write the code to check if a number is a palindrome.
public static void main(String[] args) {
4. Creating a Scanner Object: We create a Scanner
object to read input from the user.
Scanner scanner = new Scanner(System.in);
5. Reading Input from the User: We prompt the user to enter a number and store this value in a variable.
System.out.print("Enter a number: ");
int number = scanner.nextInt();
6. Checking if the Number is a Palindrome: We call the isPalindrome
method to check if the number is a palindrome. The result is stored in a boolean variable isPalindrome
.
boolean isPalindrome = isPalindrome(number);
7. Printing the Result: We print whether the number is a palindrome based on the value of isPalindrome
.
if (isPalindrome) {
System.out.println(number + " is a palindrome.");
} else {
System.out.println(number + " is not a palindrome.");
}
8. Method to Check if a Number is a Palindrome: The isPalindrome
method reverses the number and compares it with the original number. If they are the same, the number is a palindrome.
public static boolean isPalindrome(int n) {
int originalNumber = n;
int reversedNumber = 0;
// Reverse the number
while (n != 0) {
int digit = n % 10;
reversedNumber = reversedNumber * 10 + digit;
n /= 10;
}
// Compare the original number with the reversed number
return originalNumber == reversedNumber;
}
Explanation of the isPalindrome
Method
- Storing the Original Number: We store the original number in a variable
originalNumber
.
int originalNumber = n;
4. Reversing the Number: We initialize a variable reversedNumber
to 0. We use a while
loop to reverse the digits of the number. In each iteration, we extract the last digit of the number using n % 10
, add it to reversedNumber
, and then remove the last digit from n
by dividing it by 10.
int reversedNumber = 0;
while (n != 0) {
int digit = n % 10;
reversedNumber = reversedNumber * 10 + digit;
n /= 10;
}
3. Comparing the Original and Reversed Numbers: We compare the original number with the reversed number. If they are equal, the number is a palindrome, and we return true
. Otherwise, we return false
.
return originalNumber == reversedNumber;
Running the Program
To run the program, follow these steps:
- Save the code in a file named
PalindromeCheck.java
. - Open a command prompt or terminal and navigate to the directory where you saved the file.
- Compile the program using the following command:
javac PalindromeCheck.java
4. Run the compiled program using the following command:
java PalindromeCheck
- Follow the prompt to enter a number. The program will display whether the number is a palindrome or not.
Example Output
If you run the program and enter 121 as the number, the output will be:
Enter a number: 123
123 is not a palindrome.
What did we learn from this article?
Checking if a number is a palindrome is a fundamental exercise that helps in understanding loops and conditional statements in programming. This Java program demonstrates how to implement a palindrome check by reversing a number and comparing it with the original. By practicing such basic programs, you can strengthen your problem-solving skills and gain confidence in your ability to write efficient Java code. Keep practicing and exploring more problems to enhance your programming skills.
Top 100 Java Programs: Click Here
Are you looking for a job: Click Here