Kaprekar Constant

22 Jun, 2022
6174 is the Kaprekar Constant. This number is special as we always get this number when following steps are followed for any four digit number such that all digits of number are not same, i.e., all four digit numbers excluding (0000, 1111, ...) 
Sort four digits in ascending order and store result in a number “asc”.Sort four digits in descending order and store result in a number “desc”.Subtract number larger number from smaller number, i.e., abs(asc – desc).Repeat above three steps until the result of subtraction doesn’t become equal to the previous number.
Sort four digits in ascending order and store result in a number “asc”.
Sort four digits in descending order and store result in a number “desc”.
Subtract number larger number from smaller number, i.e., abs(asc – desc).
Repeat above three steps until the result of subtraction doesn’t become equal to the previous number.
We always end up with 6174.Following is the program to demonstrate the same.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to demonstrate working of// Kaprekar constant#include<bits/stdc++.h>using namespace std; // This function checks validity of kaprekar's// constant. It returns kaprekar's constant for// any four digit number "n" such that all digits// of n are not same.int kaprekarRec(int n, int &prev){    if (n == 0)       return 0;     // Store current n as previous number    prev = n;     // Get four digits of given number    int digits[4];    for (int i=0; i<4; i++)    {       digits[i] = n%10;       n = n/10;    }     // Sort all four digits in ascending order    // And giet in the form of number "asc"    sort(digits, digits+4);    int asc = 0;    for (int i=0; i<4; i++)       asc = asc*10 + digits[i];     // Get all four digits in descending order    // in the form of number "desc"    sort(digits, digits+4, std::greater<int> ());    int desc = 0;    for (int i=0; i<4; i++)       desc = desc*10 + digits[i];     // Get the difference of two numbers    int diff = abs(asc - desc);     // If difference is same as previous, we have    // reached kaprekar's constant    if (diff == prev)        return diff;     // Else recur    return kaprekarRec(diff, prev);} // A wrapper over kaprekarRec()int kaprekar(int n){    int prev = 0;    return kaprekarRec(n, prev);} // Driver codeint main(){    // Trying few four digit numbers, we    // always get 6174    cout << kaprekar(1000) << endl;    cout << kaprekar(1112) << endl;    cout << kaprekar(9812) << endl;    return 0;}
// Java program to demonstrate working of// Kaprekar constantimport java.util.Arrays; class GFG{// This function checks validity of kaprekar's// constant. It returns kaprekar's constant for// any four digit number "n" such that all digits// of n are not same.static int kaprekarRec(int n, int prev){    if (n == 0)    return 0;     // Store current n as previous number    prev = n;     // Get four digits of given number    int[] digits=new int[4];    for (int i=0; i<4; i++)    {    digits[i] = n%10;    n = n/10;    }     // Sort all four digits in ascending order    // And giet in the form of number "asc"    Arrays.sort(digits);    int asc = 0;    for (int i=0; i<4; i++)    asc = asc*10 + digits[i];     // Get all four digits in descending order    // in the form of number "desc"    Arrays.sort(digits);    int desc = 0;    for (int i=3; i>=0; i--)    desc = desc*10 + digits[i];     // Get the difference of two numbers    int diff = Math.abs(asc - desc);     // If difference is same as previous, we have    // reached kaprekar's constant    if (diff == prev)        return diff;     // Else recur    return kaprekarRec(diff, prev);} // A wrapper over kaprekarRec()static int kaprekar(int n){    int prev = 0;    return kaprekarRec(n, prev);} // Driver codepublic static void main(String[] args){    // Trying few four digit numbers, we    // always get 6174    System.out.println(kaprekar(1000));    System.out.println(kaprekar(1112));    System.out.println(kaprekar(9812));}}// This code is contributed by mits
# Python3 program to demonstrate# working of Kaprekar constant # This function checks validity of# kaprekar's constant. It returns# kaprekar's constant for any four# digit number "n" such that all# digits of n are not same.def kaprekarRec(n, prev):     if (n == 0):        return 0;     # Store current n as previous    # number    prev = n;     # Get four digits of given number    digits = [0] * 4;    for i in range(4):        digits[i] = n % 10;        n = int(n / 10);     # Sort all four digits in ascending order    # And giet in the form of number "asc"    digits.sort();    asc = 0;    for i in range(4):        asc = asc * 10 + digits[i];     # Get all four digits in descending order    # in the form of number "desc"    digits.sort();    desc = 0;    for i in range(3, -1, -1):        desc = desc * 10 + digits[i];     # Get the difference of two numbers    diff = abs(asc - desc);     # If difference is same as previous,    # we have reached kaprekar's constant    if (diff == prev):        return diff;     # Else recur    return kaprekarRec(diff, prev); # A wrapper over kaprekarRec()def kaprekar(n):     rev = 0;    return kaprekarRec(n, rev); # Driver code # Trying few four digit numbers, # we always get 6174print(kaprekar(1000));print(kaprekar(1112));print(kaprekar(9812)); # This code is contributed by mits.
// C# program to demonstrate working of// Kaprekar constantusing System; class GFG{// This function checks validity of kaprekar's// constant. It returns kaprekar's constant for// any four digit number "n" such that all digits// of n are not same.static int kaprekarRec(int n, int prev){    if (n == 0)    return 0;     // Store current n as previous number    prev = n;     // Get four digits of given number    int[] digits=new int[4];    for (int i=0; i<4; i++)    {    digits[i] = n%10;    n = n/10;    }     // Sort all four digits in ascending order    // And giet in the form of number "asc"    Array.Sort(digits);    int asc = 0;    for (int i=0; i<4; i++)    asc = asc*10 + digits[i];     // Get all four digits in descending order    // in the form of number "desc"    Array.Sort(digits);    int desc = 0;    for (int i=3; i>=0; i--)    desc = desc*10 + digits[i];     // Get the difference of two numbers    int diff = Math.Abs(asc - desc);     // If difference is same as previous, we have    // reached kaprekar's constant    if (diff == prev)        return diff;     // Else recur    return kaprekarRec(diff, prev);} // A wrapper over kaprekarRec()static int kaprekar(int n){    int prev = 0;    return kaprekarRec(n, prev);} // Driver codepublic static void Main(){    // Trying few four digit numbers, we    // always get 6174    System.Console.WriteLine(kaprekar(1000));    System.Console.WriteLine(kaprekar(1112));    System.Console.WriteLine(kaprekar(9812));}}// This code is contributed by mits
<?php// PHP program to demonstrate working of// Kaprekar constant // This function checks validity of kaprekar's// constant. It returns kaprekar's constant// for any four digit number "n" such that// all digits of n are not same.function kaprekarRec($n, $prev){    if ($n == 0)    return 0;     // Store current n as previous number    $prev = $n;     // Get four digits of given number    $digits = array_fill(0, 4, 0);    for ($i = 0; $i < 4; $i++)    {        $digits[$i] = $n % 10;        $n = (int)($n / 10);    }     // Sort all four digits in ascending order    // And giet in the form of number "asc"    sort($digits);    $asc = 0;    for ($i = 0; $i < 4; $i++)    $asc = $asc * 10 + $digits[$i];     // Get all four digits in descending order    // in the form of number "desc"    rsort($digits);    $desc = 0;    for ($i = 0; $i < 4; $i++)    $desc = $desc * 10 + $digits[$i];     // Get the difference of two numbers    $diff = abs($asc - $desc);     // If difference is same as previous,    // we have reached kaprekar's constant    if ($diff == $prev)        return $diff;     // Else recur    return kaprekarRec($diff, $prev);} // A wrapper over kaprekarRec()function kaprekar($n){    $rev = 0;    return kaprekarRec($n, $rev);} // Driver code // Trying few four digit numbers, we// always get 6174echo kaprekar(1000) . "\n";echo kaprekar(1112) . "\n";echo kaprekar(9812) . "\n"; // This code is contributed by mits.?>
<script> // Javascript program to demonstrate working of// Kaprekar constant // This function checks validity of kaprekar's// constant. It returns kaprekar's constant for// any four digit number "n" such that all digits// of n are not same.function kaprekarRec(n , prev){    if (n == 0)    return 0;     // Store current n as previous number    prev = n;     // Get four digits of given number    var digits= Array.from({length: 4}, (_, i) => 0);    for (i=0; i<4; i++)    {    digits[i] = n%10;    n = parseInt(n/10);    }     // Sort all four digits in ascending order    // And giet in the form of number "asc"    digits.sort();    var asc = 0;    for (i=0; i<4; i++)    asc = asc*10 + digits[i];     // Get all four digits in descending order    // in the form of number "desc"    digits.sort();    var desc = 0;    for (i=3; i>=0; i--)    desc = desc*10 + digits[i];     // Get the difference of two numbers    var diff = Math.abs(asc - desc);     // If difference is same as previous, we have    // reached kaprekar's constant    if (diff == prev)        return diff;     // Else recur    return kaprekarRec(diff, prev);} // A wrapper over kaprekarRec()function kaprekar(n){    var prev = 0;    return kaprekarRec(n, prev);} // Driver code //Trying few four digit numbers, we// always get 6174document.write(kaprekar(1000)+"<br>");document.write(kaprekar(1112)+"<br>");document.write(kaprekar(9812)+"<br>");  // This code contributed by Princi Singh </script>
Output : 
6174
6174
6174
Illustration :  
n = 2324
1) asc  = 2234
2) desc = 4322
3) Difference = 2088 
4) Repeating above steps as difference is not same
as n

n = 2088
1) asc  = 0288
2) desc = 8820
3) Difference = 8532  
4) Repeating above steps as difference is not same
as n.

n = 8532
1) asc  = 2358
2) desc = 8532
3) Difference = 6174  
4) Repeating above steps as difference is not same
as n.

n = 6174
1) asc  = 1467
2) desc = 7641
3) Difference = 6174  
Stopping here as difference is same as n.
Time Complexity: O(nlogn) where n is the number of digits.
Reference: https://en.wikipedia.org/wiki/6174_(number)Related Article: Kaprekar NumberThis article is contributed by Gaurav Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Mithun Kumar
princi singh
hritikbhatnagar2182
surindertarika1234
kumargaurav97520
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.