Palindrome Number Check in Java: Explanation & Practice

Check if a number is a palindrome

Problem summary

Write a program that checks if a number reads the same forwards and backwards.

Starter code

public class Main {
    public static void main(String[] args) {
        int number = 12321; // Test case 1
        
        // Check if number is a palindrome
        // Print: Is palindrome: <true/false>
    }
}

Expected output and test cases

  • 12321 is a palindrome
    Is palindrome: true
  • 12345 is not a palindrome
    Is palindrome: false
  • 7 is a palindrome
    Is palindrome: true

Hints

  1. Reverse the number and compare with original
  2. You can also compare digits from both ends
  3. Single digit numbers are always palindromes

Related Core Java Basics exercises

Practice all Core Java Basics exercises · Run this idea in the Java compiler