Pages

Java program to Count number of digit in an Integer

 Count digit in a given number  👐👐👐!!!!!!!!!!!!!!!!!


Example - 

if given number -->

X = 7654;

result ->  4


X = 765;

result ->  3


X = 76;

result ->  2



public class CountDigit {
    public static void main(String[] args) {
        //count digits in a given number

        int x = 9876;
        int result =0;
        while(x>0)
        {
            x = x/10;
            result ++;
        }

        System.out.println("Total digit -> "+result);
        //in this case result will be 4 
    }
}
     
Time complexity: θ(d)
where d is the number of the digit in the input number.

Find another must-read post.




No comments:

Post a Comment