1,105,226 Community Members

Counting Integers Algorithm

Member Avatar
Matth963
Light Poster
25 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
int numdigits(int num){

int len = 0;

while(num >= 10){
num = num/10;
len++;
}

return len;
}

This algorithm is used to count the number of digits but I can't understand it.
Someone explain it to me please :D

Thanks

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Add print statements after each line to print out the values that were set/changed on that line. Then you'll be able to understand how it works.

Member Avatar
Matth963
Light Poster
25 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a lot!

Member Avatar
hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 164 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
0
 

Well... It works, but it will be 1 digit short.

Question Answered as of 2 Years Ago by JamesCherrill and hfx642
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: