In this program, we will find the frequency of each digit in a given integer.
Steps to solve the program
- Take a number as input and convert it into a string using str() and store it in another variable.
- Create an empty dictionary.
- Use for loop to iterate over each digit of the number and digit as key and its occurrences as the value in the dictionary.
- Print the output
num = 12312543
str1 = str(num)
dict1 = {}
for val in str1:
if val in dict1:
dict1[val] += 1:
else:
dict1[val] = 1
print(dict1)
Output :
{'1': 2, '2': 2, '3': 2, '5': 1, '4': 1}