I need to store the number of digits from a number into a variable. e.g. 538, i need to store 3. How can I accomplish this is C++? Thanks.

Recommended Answers

All 2 Replies

Here is something to start with:

int getNrDigits(int my_int)
{
    int local_int = my_int;
    int count = 0;

    while (local_int > 0)
    {
        local_int /= 10;
        count++;
    }

    return count;
}

I hope I didn't missed anything because this was programming on paper at late hour as somebody called it once. :)

It is interesting to note the relationship between log(base 10) of an integer and the number of digits in that integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.