Demonstration of Kaprekar's Constant Given any four-digit number that consists of at least two different digits, if you form a number by arranging its digits in descending order, and subtract the number formed by arranging its digits in ascending order, and repeat, you should eventually arrive at 6174, Kaprekar's Constant.

How can I make improvements to my code? Is my code readable and simple to understand?

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;functional&gt;

//at least two digits must be distinct
bool isUnique(int num)
{
constexpr int numlength = 4;
std::vector&lt;int&gt; digits;

for(int i = 0; i &lt; numlength ; ++i)
{
    int digit = num%10;
    digits.push_back(digit);
    num /= 10;
}

//If digits are all equal return false.
if ( std::adjacent_find( digits.begin(), digits.end(), std::not_equal_to&lt;int&gt;() ) == digits.end() )
{
    return false;
}else
{
    return true;
}

}

int KaprekarsConstant(int num)
{
constexpr int kaprekarsConst = 6174;
constexpr int lengthRequirement = 4;

static int iteration = 0;

if(std::to_string(num).length() == lengthRequirement &amp;&amp; isUnique(num))
{
    int difference = num;
    std::string sNum;

        while(difference != kaprekarsConst)
        {
            sNum = std::to_string(difference);

            std::sort(sNum.begin(),sNum.end(),std::greater&lt;int&gt;());
            int ascendNum = stoi(sNum);

            std::sort(sNum.begin(),sNum.end(),std::less&lt;int&gt;());
            int descendNum = stoi(sNum);

            difference = ascendNum - descendNum;

            ++iteration;
        }
}
return iteration;
}
I'm assuming the indenting is cut and paste problems, but going through this:

#include &lt;iostream&gt;


You don't use iostream, so don't include it

#include &lt;string&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;functional&gt;


I'd recommend you sorted the #includes. It makes it a lot easier to find things if you have a lot of them and avoids a certain class of problem with merging file changes

//at least two digits must be distinct
bool isUnique(int num)


Given the comment, isUnique is not a good name. It's looking for 2 distinct digits, not - well, it's not looking for anything unique. contains_2_distinct_digits perhaps?

{
    constexpr int numlength = 4;
    std::vector&lt;int&gt; digits;

    for(int i = 0; i &lt; numlength ; ++i)


for is not a function call. It's a keyword. Put a space after it.

    {
        int digit = num%10;
        digits.push_back(digit);
        num /= 10;
    }


The 4 doesn't really belong here (see below).

    //If digits are all equal return false.
    if ( std::adjacent_find( digits.begin(), digits.end(), std::not_equal_to&lt;int&gt;() ) == digits.end() )


I wouldn't have the space after the ( there. 

This code is somewhat inconsistent with the use of spaces. You have a space after commas and brackets here, but not further down. Pick a style and stick to it.

    {
        return false;
    }else


I'd recommend against dangling the else off the }. Either put it on its own line or use '} else {'

    {
        return true;
    }


You don't really need that if. Just 

    return std::adjacent_find(digits.begin(), digits.end(), std::not_equal_to&lt;int&gt;()) != digits.end();

}

int KaprekarsConstant(int num)


TBH I'd expect a name like that to return the constant (actually I wouldn't expect with that name it'd even be a function). This isn't returning that. It's returning how many iterations it takes to stabilise.

{
    constexpr int kaprekarsConst = 6174;
    constexpr int lengthRequirement = 4;


You have this 4 here and you use 4 for the passing in isUnique. This is fragile, should you ever want to deal with a similar property for numbers with digits other than 4.

    static int iteration = 0;


There's no need for this to be static

    if(std::to_string(num).length() == lengthRequirement &amp;&amp; isUnique(num))


if is not a function call

    {
        int difference = num;
        std::string sNum;


sNum only needs to exist in the while loop, so put it there. You should attempt to declare variables at the point of use where possible.

            while(difference != kaprekarsConst)


while is not a function call. Put a space after it.

If you wanted to generalise this to n digits, I wouldn't define kaprekarsConst. You'd just check if the newly evaluated difference matched the previous difference.

            {
                sNum = std::to_string(difference);

                std::sort(sNum.begin(),sNum.end(),std::greater&lt;int&gt;());
                int ascendNum = stoi(sNum);

                std::sort(sNum.begin(),sNum.end(),std::less&lt;int&gt;());
                int descendNum = stoi(sNum);


As has been suggested, this is already sorted, so you could just reverse it.

                difference = ascendNum - descendNum;

                ++iteration;
            }
    }
    return iteration;
}