Kaprekar's constant algorithm in c# I have implemented the kaprekar's constant algorithm in c#, and I want your feedback.

Here is how the algorithm works:


it takes a number of 4 digits (e.g: 5823) 
we order that number in ascendent and descendent order, which gives as two numbers (e.g: 8532 and 2358).
we subtract the larger number from the smaller number (e.g: 8532 - 2358 = 6174) 
we repeat this operation until the difference is equal to 6174.


Example:

step 1:

 number = 8532
    cpt = 0 // the number of repeatation util the number became equal to 6174.
    1) asc  = 2358
    2) desc = 8532
    3) Difference = 6174  
    4) cpt++ 
    5) Repeating above steps as difference is not equal to number.


step2:

 number = 6174
     1) asc  = 1467
     2) desc = 7641
     3) Difference = 6174
     4) c++  
     5) return cpt and stopping here as difference is equal to number.


Here is my Code:

1- recursive method:

   int KaprekarsConstant(int num, int target)
        {
            if (num.ToString().Length != 4)
                throw new ArgumentException();

            var desendingOrder = num.ToString().OrderByDescending(a =&gt; a);
            var acendingOrder = num.ToString().OrderBy(a =&gt; a);

            int maxPart = int.Parse(new string(desendingOrder.ToArray()));
            int minPart = int.Parse(new string(acendingOrder.ToArray()));

            var diff = maxPart - minPart;
            if (diff == target) return 1;
            return 1+ KaprekarsConstant(diff, target);
        }


2- non-recursive method:

 int KaprekarsConstant2(int num, int target)
            {
                int cpt = 0;
                if (num.ToString().Length != 4)
                    throw new ArgumentException();

                while (true)
                {
                    var desendingOrder = num.ToString().OrderByDescending(a =&gt; a);
                    var acendingOrder = num.ToString().OrderBy(a =&gt; a);

                    int maxPart = int.Parse(new string(desendingOrder.ToArray()));
                    int minPart = int.Parse(new string(acendingOrder.ToArray()));

                    var diff = maxPart - minPart;
                    cpt++;
                    if (diff == target) return cpt;
                    num = diff;
                }
            }


Code test:

  public static void Main(string[] args)
        {
            Console.WriteLine(KaprekarsConstant(6174, 6174)); //cpt =1
            Console.WriteLine(KaprekarsConstant(3524, 6174)); //cpt =3
            Console.WriteLine(KaprekarsConstant(2324, 6174)); //cpt =3
            Console.WriteLine(KaprekarsConstant(9812, 6174)); //cpt =2
            Console.ReadLine();
        }


Thanks in advance for your help!
6174 is Kaprekar Constant, I don't know why you're using it as an
input argument  while it should be within the method. So, you'll only
need to pass one int argument.
there is no need to call ToString to get the length of the integer.
You can check the integer range instead. So, this num.ToString().Length != 4 can be converted to num &gt;= 1000 &amp;&amp; num &lt;= 9999.
using LINQ is fine, but in your case, I don't see a need of that. As you have fixed input length (4 digits), you can simply converted to string, then just do str[index] where the index would be a range of 0 to 3.
why are you using while(true) while you could enforce kaprekar's
constant condition by using while(diff != 6174).
No need for recursive method.


here is a standard version : 

int KaprekarsConstant(int num)
{

    // zero == zero
    if (num == 0) { return 0; }

    // if num is not between 1000 and 9999 throw exception
    if(num &gt;= 1000 &amp;&amp; num &lt;= 9999) { throw new ArgumentException(); }

    // will be used inside the loop
    var diff = num;

    // number of repeatation
    var numberOfRepeatation = 0;

    do // do it at least once.
    {
        //to array 
        var digitsAsc = new int[4];

        for (var x = 0; x &lt; 4; x++)
        {
            digitsAsc[x] = diff % 10;
            diff /= 10;
        }

        // sort the elements in the array to ascendent order
        Array.Sort(digitsAsc);

        //now, digitsAsc array is in ascedent order, we will resorted into descendent order
        var digitsDesc = new int[4];

        for (var x = 0; x &lt; 4; x++)
        {
            digitsDesc[x] = digitsAsc[3 - x];
        }

        // convert them to int
        var asce = int.Parse(string.Join(string.Empty, digitsAsc));

        var desc = int.Parse(string.Join(string.Empty, digitsDesc));

        diff = desc - asce;

        numberOfRepeatation++;

    }
    while (diff != 6174);

    return numberOfRepeatation;
}


if you're into LINQ, here is LINQ version : 

int KaprekarsConstant(int num)
{
    // zero == zero
    if (num == 0) { return 0; }

    // if num is not between 1000 and 9999 throw exception
    if(num &gt;= 1000 &amp;&amp; num &lt;= 9999) { throw new ArgumentException(); }

    // will be used inside the loop
    var diff = num;

    // number of repeatation
    var numberOfRepeatation = 0;

    do // do it at least once.
    {
        //to ascedent array 
        var digitsAsc = Array
            .ConvertAll(diff.ToString().ToCharArray(), x =&gt; (int)char.GetNumericValue(x))
            .OrderBy(x=&gt;x)
            .ToArray();

        //now, digitsAsc array is in ascedent order, we will resorted into descendent order by calling Reverse;
        var digitsDesc = digitsAsc.Reverse().ToArray();

        // convert them to string (this is similar to string.Join)
        var asce = digitsAsc
            .Aggregate(new StringBuilder(), (x, y) =&gt; x.Append(x.Length == 0 ? string.Empty : string.Empty).Append(y))
            .ToString();

        var desc = digitsDesc
            .Aggregate(new StringBuilder(), (x, y) =&gt; x.Append(x.Length == 0 ? string.Empty : string.Empty).Append(y))
            .ToString();

        diff = int.Parse(desc) - int.Parse(asce);

        numberOfRepeatation++;

    }
    while (diff != 6174);

    return numberOfRepeatation;
}