-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfDigits.java
More file actions
38 lines (30 loc) · 964 Bytes
/
NumberOfDigits.java
File metadata and controls
38 lines (30 loc) · 964 Bytes
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
import java.util.Scanner;
class NumberOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
long d = printNoOfDigitsIterative(a);
long e = printNoOfDigitsRecursive(b);
long f = printNoOfDigitsLogarthamic(c);
System.out.println(d + " " + e + " " + f);
sc.close();
}
public static long printNoOfDigitsIterative(long n) {
long count = 0;
while (n > 0) {
n /= 10;
count++;
}
return count;
}
public static long printNoOfDigitsRecursive(long n) {
if (n == 0)
return 0;
return 1 + printNoOfDigitsRecursive(n / 10);
}
public static long printNoOfDigitsLogarthamic(long n) {
return (long) Math.floor(Math.log10(n) + 1);
}
}