Digit Frequency Program in Java and Python
Digit frequency counting program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to input a number. Count and print the frequency of each digit present in that number.
Algorithm:
Step 1: Start.
Step 2: Accept a number from the user.
Step 3: Create an integer array freq of size 10.
Step 4: Initialize all positions of freq to 0.
Step 5: Repeat while the number is greater than 0.
Step 6: Extract the last digit using d = n % 10.
Step 7: Increase freq[d] by 1.
Step 8: Remove the last digit by doing integer division n = n / 10.
Step 9: Run a loop from digit 0 to 9.
Step 10: Print only those digits for which freq[i] is not zero.
Step 11: Stop.
Explanation:
The program uses an integer array freq of size 10 to count digit frequencies. Each index of the array represents one digit. For example, freq[0] stores the frequency of digit 0, freq[1] stores the frequency of digit 1, and so on up to freq[9].
The number is processed from right to left. In each pass of the while loop, the last digit is extracted using n % 10. Suppose the extracted digit is 7; then freq[7] is increased by 1. After counting the digit, integer division by 10 removes that last digit from the number. The loop continues until all digits have been removed and n becomes 0.
After the counting stage, the program scans the frequency array from index 0 to 9. If freq[i] is not zero, it means digit i occurred in the original number. Only such digits are printed along with their frequencies. This avoids displaying unnecessary rows for digits that do not appear in the input.
The array-based approach is efficient and easy to trace. Instead of using ten separate variables for ten digits, one array stores all frequencies. The digit extracted from the number directly decides which position of the array must be increased. This is why freq[d]++ is powerful: if the digit is 5, index 5 is updated; if the digit is 0, index 0 is updated. The final loop through the array is separate from the counting loop, so the program first collects all information and then prints it in organised order.
Java Program:
/**
* The class Digit_Freq inputs a number and counts the frequency of each digit present in it
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Digit_Freq
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number: ");
int n = sc.nextInt();
int freq[] = new int[10]; // Stores frequency of digits 0 to 9.
// Display the frequency table for digits that occur at least once.
for(int i = 0; i < 10; i++)
{
freq[i] = 0;
}
// Extract digits one by one from the right side of the number.
while(n > 0)
{
int d = n % 10; // Extract last digit.
freq[d]++; // Increase frequency of that digit.
n = n / 10; // Remove the digit that has just been counted.
}
System.out.println("Output:");
System.out.println("====================");
System.out.println("Digit\tFrequency");
System.out.println("====================");
for(int i = 0; i < 10; i++)
{
if(freq[i] != 0)
System.out.println(i + "\t" + freq[i]);
}
}
}Equivalent Python Program:
n = int(input("Enter a number: "))
freq = [0] * 10
# List index 0 stores frequency of digit 0, index 1 stores digit 1, and so on.
# Extract every digit from the number using remainder by 10.
while n > 0:
d = n % 10
freq[d] = freq[d] + 1
n = n // 10
# Display only those digits which actually occur in the number.
for i in range(10):
if freq[i] > 0:
print(i, "=", freq[i])Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.