Java (programming language)

Java Program to check Given Number is Palindrome or not

Introduction

Palindromes are numbers or words that read the same backward as forward. Checking for palindromes is a common programming task that involves analyzing digits and their order. Java provides the tools to efficiently determine whether a given number is a palindrome.

Understanding Palindromes in Numbers

  • Definition: A palindromic number remains unchanged when its digits are reversed.
  • Examples: 121, 4554, 1001, and 3443 are all palindromes.

Steps to Check for Palindromes

  1. Obtain Input: Get the number to be checked from the user.
  2. Reverse Digits: Create a reversed version of the number.
  3. Compare Original and Reversed: If the original and reversed numbers are equal, it’s a palindrome.

Java Code Implementation

Java

import java.util.Scanner;

public class PalindromeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        int reversedNum = 0, remainder, originalNum;
        originalNum = num;

        // Reverse the number
        while (num != 0) {
            remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }

        // Check for palindrome
        if (originalNum == reversedNum) {
            System.out.println(originalNum + " is a palindrome.");
        } else {
            System.out.println(originalNum + " is not a palindrome.");
        }
    }
}

Explanation:

  1. Import Scanner: Import the Scanner class for user input.
  2. Prompt for Input: Ask the user to enter a number.
  3. Store Original Number: Store the input in originalNum.
  4. Reverse Digits:
    • Initialize reversedNum to 0.
    • Use a while loop to extract digits from the original number.
    • Multiply reversedNum by 10 and add the extracted digit to form the reversed number.
  5. Check for Palindrome: Compare originalNum and reversedNum for equality.
  6. Print Result: Display whether the number is a palindrome or not.

Additional Considerations

  • Large Numbers: For very large numbers, consider using long data type (long) to prevent overflow.
  • Alternative Reversal Methods: You can use recursion or string manipulation for reversing digits.
  • Optimisation: The reversal process can be stopped halfway if the number of digits is even, as the first half will mirror the second half in a palindrome.

Conclusion

Java offers a straightforward approach to identify palindromic numbers through digit reversal and comparison. By following the steps outlined and understanding the code logic, you can effectively create Java programs to check for palindromes and enhance your number manipulation skills.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button