Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Number of digits

Easy
0/40
Average time to solve is 10m
profile
Contributed by
98 upvotes
Asked in company
Tata Consultancy Services (TCS)

Problem statement

You are given a number 'n'.


Return number of digits in ‘n’.


Example:
Input: 'n' = 123

Output: 3

Explanation:
The 3 digits in ‘123’ are 1, 2 and 3. 


Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘n’.


Output Format:
Return an integer as described in the problem statement. 


Note
You don’t need to print anything, it has already been taken care of, just complete the given function.
Sample Input 1:
121


Sample Output 1:
3


Explanation of sample output 1:
There are 3 digits in 121 are 1,2 and 1.


Sample Input 2:
38


Sample Output 2:
2


Expected time complexity :
The expected time complexity is O(log n).


Constraints:
1 <= ‘n’ <= 10^9
Hint

Convert the integer to a string.

Approaches (3)
Convert to String

Approach:

We can convert the integer to a string, then return the length of the string.


 

The steps are as follows:

function countDigits(int n)

  1. Convert ‘n’ to string ‘s’
  2. return s.length()
Time Complexity

O(log(n)), where ‘n’ is the given number.


 

We are iterating through all the digits of ‘n’ and there are log(n) such digits.


 

Hence, the time complexity is O(log(n)).

Space Complexity

O(log(n)), where ‘n’ is the given number.


 

We are using extra string ‘s’ of length log(n).


 

Hence, the space complexity is O(log(n)).

Code Solution
(100% EXP penalty)
Number of digits
All tags
Sort by
Search icon

Interview problems

Easy peasy python solution

import math
def countDigits(n: int) -> int:
    # Write your code here
    return int(math.log10(n)+1)
    pass
8 views
0 replies
0 upvotes

Interview problems

VERY SMALL CODE || C++ 🔥

int countDigits(int n){

int count = 0;

while(n>0){

count = count +1;

n=n/10;

}

return count;

}

16 views
0 replies
0 upvotes

Interview problems

Number of digits

int countDigits(int n){

    int count = 0;

    while(n>0){

        int lastdigit = n%10;

        count = count +1;

        n = n/10;

    }   

    

    return count;

}

20 views
0 replies
0 upvotes

Interview problems

Number of digits

int countDigits(int n){

    // Write your code here.

    int count = 0;

    while(n>0)

    {

        count = count+1;

        n = n/10;

    }   

    return count;

}

17 views
0 replies
0 upvotes

Interview problems

JAVA Solution

import java.util.*; 

 

public class Solution { 

 

public static int countDigits(int n) { 

 

// Handle the special case where n is 0 

 

if (n == 0) {

 return 1;

 } 

 

// Calculate the number of digits 

 

int count = (int)(Math.log10(n) + 1); 

 

return count; 

   }

}

23 views
0 replies
1 upvote

Interview problems

JAVA EASY SOLUTION

public class Solution {

    public static int countDigits(int n){

        // Write your code here.

        int count = 0;

        while (n > 0) {

        n = n / 10 ;

        count++;    

        }

        return count;

    }

}

12 views
0 replies
0 upvotes

Interview problems

how to run this code in java

public class Solution {

    public static int countDigits(int n){

        int lastdigit=0;

        int count=0;

        while(n>0)

        {

            lastdigit=n%10;

            count=count+1;

            n=n/10;

        }

        return count;

    }

}

13 views
0 replies
0 upvotes

Interview problems

[C++] All Tests passed

int countDigits(int n){
	// Write your code here.	

	// corner cases
	if(n == 0) return 1;

	int count = 0;

	while(n > 0){

		n = n / 10;
		count++;
	}

	return count;
}
92 views
0 replies
0 upvotes

Interview problems

Easiest One Line solution

int countDigits(int n){

    // Write your code here.

    int count = to_string(n).length();

    return count;   

}

48 views
0 replies
1 upvote

Interview problems

C++ Solution || All test case passed || Time Complexity: O(1)

int countDigits(int n){

    return log10(n) + 1;

}

 

45 views
0 replies
1 upvote
Full screen
Console