The Kaprekar Routine
HardAcc. 72.3%
+45 XP 25
The Universal Constant
The Kaprekar constant 6174 is reached by repeatedly:
- Taking any 4-digit number (at least two different digits).
- Sorting the digits in descending and ascending order to get two numbers.
- Subtracting the smaller from the larger.
- Repeating for the new result.
The Assignment
Your function receives a 4-digit num.
- Use a
whileloop that runs as long as the current result is not 6174. - Inside:
- Extract the 4 digits and sort them.
- Calculate the difference.
- Print the difference.
- The loop stops when 6174 is hit.
01EXAMPLE 1
Input
num = 3524Output
3087
8352
6174Explanation: Step-by-step subtraction.
Constraints
- Assume valid 4-digit inputs.
- Print each subtraction result.
MathLoopsDigits
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
num3524
Expected Output
3087 8352 6174
Click RUN to test your solution