Count digits in a number
Java
Easy
4 views
Problem Description
Task: return how many digits are in an integer. Handle 0 properly.
Output Format
Return value
Constraints
Use loop with division.
Official Solution
static int countDigits(int x){if(x==0) return 1;int n=x<0?-x:x;int c=0;while(n>0){c++;n/=10;}return c;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!