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

Digits are: 1, 2, 2, 3, 3, 4
Frequency should be:
1 → 1 time
2 → 2 times
3 → 2 times
4 → 1 time

Logic Steps

  1. Read input as string.

  2. Create empty dictionary.

  3. Traverse each character in string.

  4. If digit already exists in dictionary:

    • Increase count

  5. Else:

    • Add digit with count 1

  6. 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

  1. Start

  2. Read number as string

  3. Initialize empty dictionary freq

  4. For each character digit in string:

    • If digit in freq

      • freq[digit] = freq[digit] + 1

    • Else

      • freq[digit] = 1

  5. Display digit and its frequency

  6. 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

Enter a multi-digit number: 122334
Digit Frequency:
Digit 1 occurs 1 times
Digit 2 occurs 2 times
Digit 3 occurs 2 times
Digit 4 occurs 1 times

Case 2

Enter a multi-digit number: 55555
Digit Frequency:
Digit 5 occurs 5 times

Important Concepts Used

  • String input

  • Dictionary data structure

  • Membership operator (in)

  • Looping through string

  • Key-value pairs


Viva Voce Questions

  1. Why is the number read as string?

  2. What is a dictionary?

  3. What is the use of in operator?

  4. What happens if input contains alphabets?

  5. How to sort digits before printing?

  6. What is the time complexity of this program?

  7. Can this be done without dictionary?


Procedure to Execute the Program

  1. Open Python IDLE / VS Code / Google Colab

  2. Create new file

  3. Type the program

  4. Save as .py

  5. Run the program

  6. 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.Counter

  • Display 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.



-:END:-

Comments

Popular posts from this blog