forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_of_digits.py
47 lines (33 loc) · 877 Bytes
/
count_of_digits.py
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
"""
Python program to get count of digits.
Count of digits refers to the sum of the frequency of all distinct digits in a number.
"""
# Function to count digits in the number
def count_of_digits(n):
if(n==0):
return 1
# Initializing cnt variable to zero
cnt = 0
while True:
if(n==0):
break
# Increasing the cnt
cnt+=1
# Removing the least significant digit
n = n//10
return cnt
if __name__ == '__main__':
# Taking Input
n = int(input("Enter the number :"))
# Printing Output
print("The count of digits in the given number is: {} ".format(count_of_digits(n)))
"""
Time Complexity - O(n), n is the count of digits in the number
Space Complexity - O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE I
INPUT
Enter the number: 8965
OUTPUT
The count of digits in the given number is: 4
"""