Kaprekar Number Checker - Problem

A Kaprekar number is a special number where if you square it and split the result into two parts, the sum of those parts equals the original number.

For example, 45 is a Kaprekar number because:

  • 45² = 2025
  • Split: 20 + 25 = 45

Write a function that checks if a given number is a Kaprekar number. The split should divide the squared number into left and right parts of equal or nearly equal length.

Note: If the left part is empty after splitting, treat it as 0.

Input & Output

Example 1 — Classic Kaprekar Number
$ Input: n = 45
Output: true
💡 Note: 45² = 2025. Split: '2025' → '20' + '25' = 20 + 25 = 45. Sum equals original, so it's a Kaprekar number.
Example 2 — Not a Kaprekar Number
$ Input: n = 10
Output: false
💡 Note: 10² = 100. Split: '100' → '1' + '00' = 1 + 0 = 1. Sum (1) ≠ original (10), so not a Kaprekar number.
Example 3 — Single Digit Special Case
$ Input: n = 1
Output: true
💡 Note: 1² = 1. Split: '1' → '' + '1' = 0 + 1 = 1. By definition, 1 is considered a Kaprekar number.

Constraints

  • 1 ≤ n ≤ 104
  • n is a positive integer

Visualization

Tap to expand
INPUTALGORITHMRESULT45Input Number1Square: 45² = 20252Convert to string: "2025"3Split in half: "20" + "25"4Convert back: 20 + 25TRUEIs Kaprekar Number20 + 25 = 45 ✓Key Insight:A number is Kaprekar if its square, split in half, adds back to the original numberTutorialsPoint - Kaprekar Number Checker | String Manipulation Approach
Asked in
Google 15 Microsoft 12 Amazon 8
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen