The Kaprekar Routine

HardAcc. 72.3%
+45 XP 25

The Universal Constant

The Kaprekar constant 6174 is reached by repeatedly:

  1. Taking any 4-digit number (at least two different digits).
  2. Sorting the digits in descending and ascending order to get two numbers.
  3. Subtracting the smaller from the larger.
  4. Repeating for the new result.

The Assignment

Your function receives a 4-digit num.

  1. Use a while loop that runs as long as the current result is not 6174.
  2. Inside:
    • Extract the 4 digits and sort them.
    • Calculate the difference.
    • Print the difference.
  3. The loop stops when 6174 is hit.

01EXAMPLE 1

Inputnum = 3524
Output3087 8352 6174

Explanation: Step-by-step subtraction.

Constraints

  • Assume valid 4-digit inputs.
  • Print each subtraction result.
MathLoopsDigits
JavaScript
Loading...
2 Hidden

Input Arguments

num3524

Expected Output

3087
8352
6174

Click RUN to test your solution