The program to implement Kaprekar Routine for n-digit numbers is based on Indian Mathematician D.R. Kaprekar constant \Large 6174 . The Kaprekar routine always converge to this constant – \Large 6174 for any \Large n digit number.
The basic rules to chose a number for Kaprekar routine is:
Once you selected a four digit number. We can apply the Kaprekar routine to it and get the end result in less than or equal to 7 steps. The final result of the routine is \Large 6174.
Suppose we choose any arbitrary number such as \Large 3968.
The number is 4-digit number and it has at no repeating digits.
Next, we must arrange the digits in descending order to get the largest possible number from the digits.
\Large 3968 \rightarrow 9863
After sorting the new number is \Large 9863.
We must arrange the digits of new number from step 2 in ascending order ( smallest to largest ). In this way, we get the smallest possible number from the digits.
\Large 9863 \rightarrow 3689
The smallest number obtained from digits of \Large 9863 are \Large 3689.
The largest number obtained and smallest number from step 3 will be used to get the Kaprekar constant.
In this step, you need the largest number obtained in step 2 and the smallest number obtained from step 3. Subtract this two numbers to get a result.
\Large \begin{aligned} & \hspace{16px} 9863 \\
&- 3689 \\
&\rule{2cm}{0.5px}\\
&\hspace{16px} 6174\\
&\rule{2cm}{0.5px}\\
\end{aligned}The step 5 is about repeating the routine until you find the Kaprekar constant which is the number \Large 6174.
If you repeat the Kaprekar routine for any n-digit number repeatedly, the end result is \Large 6174.
The Kaprekar routing move from random number to structured difference of number (Largest – Smallest) and by repeating the subtraction procedure, you reach a stable number \Large 6174.
The result of Kaprekar routine on random number \Large 3968 is the stable constant \Large 6174.
Lets take another example, \Large 4386. The Kaprekar routine will produce following numbers.
\Large \begin{aligned}
8643 - 3486 = 5175\\
7551 - 1557 = 5994\\
9954 - 4599 = 5335\\
5533 - 3355 = 1998\\
9981 - 1899 = 8082\\
8820 - 0288 = 8532\\
8532 - 2358 = 6174
\end{aligned}See the program for Kaprekar routine on a \Large 4386 below.
#include <stdio.h>
void sort(int a[])
{
int i, j, temp;
for (i = 0; i < 4; i++)
{
for (j = i + 1; j < 4; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
int main()
{
int num, a[4], b[4], c[4], i;
int count = 0;
printf("Enter a 4-digit number: ");
scanf("%d", &num);
while (num != 6174)
{
// split digits
a[0] = num / 1000;
a[1] = (num / 100) % 10;
a[2] = (num / 10) % 10;
a[3] = num % 10;
// sort ascending
sort(a);
// copy ascending to b
for (i = 0; i < 4; i++)
b[i] = a[i];
// reverse a to make descending
for (i = 0; i < 2; i++)
{
int temp = a[i];
a[i] = a[3 - i];
a[3 - i] = temp;
}
// subtraction: descending - ascending
for (i = 3; i >= 0; i--)
{
if (a[i] < b[i])
{
a[i - 1]--;
a[i] += 10;
}
c[i] = a[i] - b[i];
}
// convert array to number
num = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
count++; // increment iteration count
printf("Step %d: %d\n", count, num);
if (num == 0) break; // safety check
}
printf("\nTotal iterations = %d\n", count);
return 0;
}#include <iostream>
#include <algorithm>
using namespace std;
void sortAsc(int a[])
{
sort(a, a + 4);
}
int main()
{
int num;
int a[4], b[4], c[4];
int count = 0;
cout << "Enter a 4-digit number: ";
cin >> num;
while (num != 6174)
{
// split digits
a[0] = num / 1000;
a[1] = (num / 100) % 10;
a[2] = (num / 10) % 10;
a[3] = num % 10;
// sort ascending
sortAsc(a);
// copy ascending to b
for (int i = 0; i < 4; i++)
b[i] = a[i];
// reverse to descending
reverse(a, a + 4);
// subtraction: descending - ascending
for (int i = 3; i >= 0; i--)
{
if (a[i] < b[i])
{
a[i - 1]--;
a[i] += 10;
}
c[i] = a[i] - b[i];
}
// convert to number
num = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
count++;
cout << "Step " << count << ": " << num << endl;
if (num == 0)
break;
}
cout << "\nTotal iterations = " << count << endl;
return 0;
}import java.util.Arrays;
import java.util.Scanner;
public class Kaprekar {
static void sortAsc(int[] a) {
Arrays.sort(a);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
int[] a = new int[4];
int[] b = new int[4];
int[] c = new int[4];
int count = 0;
System.out.print("Enter a 4-digit number: ");
num = sc.nextInt();
while (num != 6174) {
// split digits
a[0] = num / 1000;
a[1] = (num / 100) % 10;
a[2] = (num / 10) % 10;
a[3] = num % 10;
// sort ascending
sortAsc(a);
// copy ascending to b
for (int i = 0; i < 4; i++)
b[i] = a[i];
// reverse to descending
for (int i = 0; i < 2; i++) {
int temp = a[i];
a[i] = a[3 - i];
a[3 - i] = temp;
}
// subtraction: descending - ascending
for (int i = 3; i >= 0; i--) {
if (a[i] < b[i]) {
a[i - 1]--;
a[i] += 10;
}
c[i] = a[i] - b[i];
}
// convert array to number
num = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
count++;
System.out.println("Step " + count + ": " + num);
if (num == 0)
break;
}
System.out.println("\nTotal iterations = " + count);
sc.close();
}
}def kaprekar(num):
count = 0
while num != 6174 and num != 0:
digits = list(f"{num:04d}") # ensure 4 digits with leading zeros
asc = int("".join(sorted(digits)))
desc = int("".join(sorted(digits, reverse=True)))
num = desc - asc
count += 1
print(f"Step {count}: {num}")
print("\nTotal iterations =", count)
num = int(input("Enter a 4-digit number: "))
kaprekar(num)The goal of the program is count the number of iterations to reach Kaprekar constant for any number. The program has following functions:
Enter a 4-digit number: 4386
Step 1: 5175
Step 2: 5994
Step 3: 5355
Step 4: 1998
Step 5: 8082
Step 6: 8532
Step 7: 6174
Total iterations = 7