6

I have to count the number of digits in a number.

I divide the number to 10 until I get 0. Each iteration increments the counter.

int num;
cin>>num;  
while(num > 0)  
{  
  counter++;
  num = num / 10;   
}

The challenge is not using any loops or recursion, just an if statement.

Is it possible?

Niall
  • 30,036
  • 10
  • 99
  • 142
yael aviv
  • 201
  • 1
  • 5
  • 9

6 Answers6

13

counter = log(num) / log(10)

in c++:

#include <cmath>
....
counter = num == 0 ? 1 : log10(std::abs(num)) + 1;

what you want is the log function.

cplusplus - log10

cplusplus - std::abs

MoonBun
  • 4,322
  • 3
  • 37
  • 69
  • 1
    Still broken for `num == 0` though. – Paul R Sep 08 '14 at 09:40
  • @Vladp, Does log10 have to use double variables? – yael aviv Sep 08 '14 at 09:52
  • 2
    This doesn't work for num == int min (on a 2s complement machine). I'm not sure what standard says std::abs does in that case but the answer it gives is clearly wrong. [see http://ideone.com/exvOQK] – Mike Vine Sep 08 '14 at 09:56
  • @Mike, `std::abs()` inherits from the C `stdlib` header, which specifies undefined behaviour if the result cannot be represented. – paxdiablo Sep 08 '14 at 11:52
3

Easy way although somewhat expensive, turn your number to string and take its size like the example below:

#include <iostream>
#include <string>

int main() {
  int i = 1232323223;
  std::string str = std::to_string(std::abs(i));
  std::cout << "Number of Digits: " << str.size() <<std::endl;
}

LIVE DEMO

101010
  • 41,839
  • 11
  • 94
  • 168
3

One way is to use sprintf, since it returns the number of characters emitted:

int digits(int n)
{
    char s[32];
    int len = sprintf(s, "%d", n);
    if (n < 0) len--; // NB: handle negative case
    return len;
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
3

Sure it's possible, something like (for 32-bit numbers):

int numDigitsU (unsigned int n) {
    if (n <         10) return 1;
    if (n <        100) return 2;
    if (n <       1000) return 3;
    if (n <      10000) return 4;
    if (n <     100000) return 5;
    if (n <    1000000) return 6;
    if (n <   10000000) return 7;
    if (n <  100000000) return 8;
    if (n < 1000000000) return 9;
    /*      4294967295 is 2^32-1 - add more ifs as needed
       and adjust this final return as well. */
    return 10;
}

The signed variant is a little trickier since the sign is reversed first, and you have to watch out for MININT:

int numDigitsS (int n) {
    if (n == MININT) n = MAXINT;  // same number of digits, usually.
    if (n < 0) n = -n;            // reverse sign.
    return numDigitsU (n);        // call the unsigned variant.
}

Just adjust the highest comparison value and return value based on the size of your largest unsigned int.

This should work with all allowed negative codings: two's complement, ones' complement, and sign/magnitude.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

It's actually quite easy if you consider that the maximum size of int is finite. Just check if the number is larger than 10, larger than 100, larger than 1000, etc. You could even do binary search.

int num = abs(number);
if (num < 10000)
{
    if (num < 100)
        return num < 10 ? 1:2;
    else
        return num < 1000 ? 3:4;   
}
else
{
    ...
}
Peter
  • 5,608
  • 1
  • 24
  • 43
0

You can find the length of an integer just by doing it as follows:

int countDigits(int *num){
    int count =0;
    while(*num>0){
        count++;
        *num /=10;
    }
    return count;
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
crispengari
  • 7,901
  • 7
  • 45
  • 53