Does anyone know if it's possible to count the number of digits in an integer? One of my friends recommended log10 or something but I can't figure it out.

Redneon

Printable View

- 08-18-2003redneonCount Number of Digits in and Integer
Does anyone know if it's possible to count the number of digits in an integer? One of my friends recommended log10 or something but I can't figure it out.

Redneon - 08-18-2003FillYourBrain
the number of digits in a base 10 number is equal to the number of times you multiply 1 times 10 to get to the same length (plus one to account for the first digit). So essentially, log 10 is that number. your buddy is correct.

- 08-18-2003Iamien
Here you go should work as you need

Code:

int number, temp, print;

cin>>number;

print = 0;

while (temp != 0) {

temp = number % 10;

number = number / 10;

print++;

}

print = print -1; // reason for this below

cout<<print;

IE it would count one more then needed.

try it without this and you'll see what i mean