//Time Complexity : O(d), where 'd' is the digits of number
import java.io.*;
import java.util.*;
public class CountDigits {
static int countDigits(int num)
{
int count = 0;
while(num > 0)
{
num = num / 10;
count++;
}
return count;
}
public static void main (String[] args) {
int number = 789;
System.out.println(countDigits(number));
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter