-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfDigits.java
More file actions
75 lines (48 loc) · 1.23 KB
/
NumberOfDigits.java
File metadata and controls
75 lines (48 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Number of Digits
// Send Feedback
// 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.
// 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
import java.util.Scanner;
public class NumberOfDigits {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter n : ");
int n = s.nextInt();
int ans = count(n);
System.out.println("Output : " + ans);
s.close();
}
public static int count(int n)
{
if(n<10)
{
return 1;
}
return 1 + count(n/10);
}
}