How can I get the length/count of digits in a variable? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 3

How can I get the length/count of digits in a variable?

This is C/C++. I have: long long ccNumber = 4567788875433335; In what ways can I get the total number of digits in the variable ccNumber? PLEASE HIGHLIGHT STEPS WITH PSEUDOCODE!

5th Apr 2017, 6:02 PM
Chuma Umenze
10 Respuestas
+ 5
@SAMI Awad What's the need for the d=n%10? Where: long long ccNumber = 4567788875433335; int count; while (ccNumber > 0) { count++; ccNumber /= 10; } can still get me the total number of digits. Are there any other method?
5th Apr 2017, 6:46 PM
Chuma Umenze
+ 4
While n>0{ d=n%10 cnt++ n/=10 { C cnt is your count of digits
5th Apr 2017, 6:16 PM
SAMI Awad
SAMI Awad - avatar
+ 3
@SAMI Awad, thank you for the tips.
5th Apr 2017, 7:06 PM
Chuma Umenze
+ 2
You can store (d) if you want in an array but for the count you don't need it
5th Apr 2017, 6:57 PM
SAMI Awad
SAMI Awad - avatar
+ 2
@Luca Garrera I don't think so. Count was already declared outside the loop. I did so, so I can still be able to use the variable within my main function. See this code snippet. https://code.sololearn.com/csb9nLyIRf1s/?ref=app
5th Apr 2017, 7:00 PM
Chuma Umenze
+ 1
I don't know C nor C++ but you can calculate the number's base 10 logarithm, then round the result down, then sum 1 to the result. This only works for natural numbers. Examples: log(2)=0.301 floor(0.301)=0 0+1=1 log(10)=1 floor(1)=1 1+1=2
5th Apr 2017, 6:15 PM
Luca Garrera
+ 1
@Galin McMahon that syntax looks like Javascript. I don't think C/C++ has a way of getting the length of a long long integer.
7th Apr 2017, 5:34 AM
Chuma Umenze
0
@Chuma Shouldn't count be initialized outside the loop, and the loop condition be ccNumber>=1?
5th Apr 2017, 6:57 PM
Luca Garrera
0
@Chuma I always forget about integer division, my bad ^^' I still think that count should be initialized outside the loop (as you did in your code).
5th Apr 2017, 7:11 PM
Luca Garrera
0
Have never tried counting a string but maybe (var).length
7th Apr 2017, 3:10 AM
Galin McMahon
Galin McMahon - avatar