Program3(b)
3. b. Read a multi-digit number (as chars) from the console. Develop a program to print the frequency of each digit with a suitable message.
Objective of the Program
To understand string input handling
To count frequency of characters
To use dictionary for counting
To iterate through string elements
Logical Construction (Think Before You Code)
Important: The number must be read as a string, not as an integer.
Why?
Because:
We want to process each digit separately
String allows easy iteration over characters
Example
If input is: 122334
Logic Steps
Read input as string.
Create empty dictionary.
Traverse each character in string.
If digit already exists in dictionary:
Increase count
Else:
Add digit with count 1
Print frequency of each digit.
👉 Key idea:
Use dictionary where:
key = digit
value = frequency
Flowchart (Block Diagram of Logic)
Flowchart Explanation
Start
Read number as string
Initialize empty dictionary
For each digit:
If exists → increment count
Else → add to dictionary
Print results
Stop
Algorithm
Start
Read number as string
Initialize empty dictionary
freqFor each character
digitin string:If digit in freq
freq[digit] = freq[digit] + 1
Else
freq[digit] = 1
Display digit and its frequency
Stop
Python Program
⚠ Students must complete the missing parts.
# Program to find frequency of each digit
number = input("Enter a multi-digit number: ")
freq = {}
for digit in number:
if digit in freq:
freq[digit] = freq[digit] + __________
else:
freq[digit] = __________
print("Digit Frequency:")
for key in freq:
print("Digit", key, "occurs", __________, "times")
Probable Output
Case 1
Case 2
Important Concepts Used
String input
Dictionary data structure
Membership operator (
in)Looping through string
Key-value pairs
Viva Voce Questions
Why is the number read as string?
What is a dictionary?
What is the use of
inoperator?What happens if input contains alphabets?
How to sort digits before printing?
What is the time complexity of this program?
Can this be done without dictionary?
Procedure to Execute the Program
Open Python IDLE / VS Code / Google Colab
Create new file
Type the program
Save as
.pyRun the program
Enter a multi-digit number
Google Colab – Online Execution
👉 Paste your Colab link here.
[https://colab.research.google.com/drive/1IpNSL6Q55CsSiOidOBvPkaJuJHJUm-q7?usp=sharing]
Program Explanation
Assignment for Students
Modify program to ignore non-digit characters
Sort output in ascending order
Count frequency using
collections.CounterDisplay highest frequency digit
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.

Comments
Post a Comment