954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Counting Integers Algorithm

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

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Thanks a lot!

Matth963
Newbie Poster
16 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: