Kaprekar Constant

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, …)

1. Sort four digits in ascending order and store result in a number “asc”.
2. Sort four digits in descending order and store result in a number “desc”.
3. Subtract number larger number from smaller number, i.e., abs(asc – desc).
4. 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++

 // C++ program to demonstrate working of // Kaprekar constant #include 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;     for (int i=0; i<4; i++)     {        digits[i] = n%10;        n = n/10;     }        // Sort all four dgits 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 dgits in descending order     // in the form of number "desc"     sort(digits, digits+4, std::greater ());     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 code int 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

 // Java program to demonstrate working of  // Kaprekar constant  import 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;      for (int i=0; i<4; i++)      {      digits[i] = n%10;      n = n/10;      }         // Sort all four dgits 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 dgits 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 code  public 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

 # 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 =  * 4;      for i in range(4):         digits[i] = n % 10;         n = int(n / 10);         # Sort all four dgits 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 dgits 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 6174  print(kaprekar(1000));  print(kaprekar(1112));  print(kaprekar(9812));    # This code is contributed by mits.

C#

 // C# program to demonstrate working of  // Kaprekar constant  using 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;      for (int i=0; i<4; i++)      {      digits[i] = n%10;      n = n/10;      }         // Sort all four dgits 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 dgits 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 code  public 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



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.

Related Article:
Kaprekar Number

This article is contributed by Gaurav Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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

My Personal Notes arrow_drop_up

Improved By : Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.