Kaprekar Number Check in Java: Explanation & Practice

Check if a number is a Kaprekar number

Problem summary

A Kaprekar number's square can be split into two parts that add up to the original (e.g., 45² = 2025, 20+25 = 45).

Starter code

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

Expected output and test cases

  • 45² = 2025, 20+25=45
    Is Kaprekar: true
  • 9² = 81, 8+1=9
    Is Kaprekar: true
  • 10 is not Kaprekar
    Is Kaprekar: false

Hints

  1. Calculate square of number
  2. Try splitting at different positions
  3. Right part cannot be 0

Related Control Flow exercises

Practice all Control Flow exercises · Run this idea in the Java compiler