1. Introduction
A Palindrome Number is a number that remains the same when its digits are reversed. For example, 121 is a palindrome number, but 123 is not. In this blog post, we will create a Java program to check whether a given number is a palindrome number or not.
2. Program Steps
1. Define a class named PalindromeNumber.
2. Inside this class, define the main method.
3. Inside the main method, create a Scanner object to take user input.
4. Prompt the user to enter a number.
5. Declare and initialize variables to store the original number, reversed number, and remainder.
6. Create a loop to reverse the number.
7. Compare the reversed number with the original number.
8. Print whether the number is a palindrome number or not.
3. Code Program
import java.util.Scanner; // Import Scanner class
public class PalindromeNumber { // Step 1: Define a class named PalindromeNumber
public static void main(String[] args) { // Step 2: Define the main method
Scanner sc = new Scanner(System.in); // Step 3: Create Scanner object to read input
System.out.println("Enter a number: "); // Step 4: Prompt the user to enter a number
int num = sc.nextInt(); // Read the user input
int originalNum = num; // Step 5: Store the original number
int reversedNum = 0; // Initialize reversedNum to 0
// Step 6: Create a loop to reverse the number
while (num != 0) {
int remainder = num % 10; // Get the last digit
reversedNum = reversedNum * 10 + remainder; // Reverse the number
num /= 10; // Remove the last digit
}
// Step 7: Compare the reversed number with the original number
if (originalNum == reversedNum) {
System.out.println(originalNum + " is a Palindrome Number."); // Step 8: Print the result
} else {
System.out.println(originalNum + " is not a Palindrome Number."); // Step 8: Print the result
}
}
}
Output:
Enter a number: 121 121 is a Palindrome Number.
4. Step By Step Explanation
– Step 1: A class named PalindromeNumber is defined.
– Step 2: The main method is defined inside the PalindromeNumber class.
– Step 3: A Scanner object is created to read the user input.
– Step 4: The user is prompted to enter a number.
– Step 5: The original number is stored and variables for reversedNum and remainder are initialized.
– Step 6: A while loop is used to reverse the number.
– Step 7: The reversed number is compared with the original number.
– Step 8: The program prints whether the original number is a Palindrome Number or not.