+ 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!
10 Answers
+ 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?
+ 4
While n>0{
d=n%10
cnt++
n/=10
{
C
cnt is your count of digits
+ 3
@SAMI Awad, thank you for the tips.
+ 2
You can store (d) if you want in an array but for the count you don't need it
+ 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
+ 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
+ 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.
0
@Chuma Shouldn't count be initialized outside the loop, and the loop condition be ccNumber>=1?
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).
0
Have never tried counting a string but maybe (var).length