In java, to print the frequency of digits in given number we can use loop concepts such as while loop or for loop. For a given number, we have to find the occurrence of each digit. If the input number is 12234 then output should be digit 1 is only occurred one time, digit 2 occurred 2 times and digit 3 and 4 are occurred one time.
In this article we will learn through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop concepts
Problem Statement
We have to write a java program to print the frequency of digits in a given number. The program should take a number from the user and should print the occurrence of each digits in input number and print it. This action will be done in our program. Let’s see an example,
Example:
For input integer number 112324, Output should be 1- 2, 2-2, 3-1 and 4 -1
Explanation:
Input integer number = 112324
Initializing the starting number: i=0
Count of each digit before checking the digit frequency=0
Taking each digit: number%10 =112324 = 4 is a digit (remainder)
Checking the digit is equal to i or not: digit==i
Now, if it is equal increase the count to +1 and reduce the number repeat the above process, if not equal, then directly reduce the number and repeat the above process for updated number.
Therefore, Continue the above steps until i becomes 9.
Hence, for the input number 112324, Output will become
digit Frequency
1 2
2 2
3 1
4 1
Logic to print frequency of digits
- Take an input number from the user.
- Define variable digit to store each digit of the number and initialize i to 0.
- Use a outer loop (while or for loop), to iterate through each digit from 0 to 9 (i starts at 0 and increments until it reaches 9).
- Inside the outer loop, Initialize count to 0 for counting occurrences of the current digit i.
- Create a copy num of the original number to avoid modifying the original.
- Use a nested loop (while or for loop) to extract each digit from num.
- Calculate the last digit of num using digit =num%10. If digit matches i (the current digit being checked), increment count using if statement. Update num by removing the last digit using integer division (num= num/10).
- After completing the inner loop, If count (frequency of digit i) is greater than 0, print i and count to show the digit and its frequency using if statement.
- After finishing the outer loop and printing all digit frequencies, close the scanner class.
Java program to print frequency of digits of a given number
Program 1: Using while loop
In the below program we are using while loop to print frequency of digits of a given number in Java.
import java.util.Scanner;
public class FrequencyOfDigits {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int number = s.nextInt();
System.out.println("Digit \t Frequency");
int digit;
int i = 0;
while (i <= 9) {
int count = 0;
int num = number;
while (num != 0) {
digit = num % 10;
if (digit == i) {
count++;
}
num = num / 10;
}
if (count > 0) {
System.out.println(i + "\t" + count);
}
i++;
}
s.close();
}
}
Output:
Enter a number
131823452
Digit Frequency
1 2
2 2
3 2
4 1
5 1
8 1
Java program explanation to print frequency of digits of a given number
- The program asks user for a number and stores it.
- Prints a header “Digit Frequency”.
- Loops through digits 0-9 (while loop).
- Initializes a counter for digit frequency (inner loop).
- Iterates through the entered number digit by digit:
- Extracts the last digit.
- Checks if the extracted digit matches the current loop value.
- If they match, increments the counter for that digit.
- Removes the last digit for next iteration.
- Prints the digit and its frequency if it appeared at least once.
- Closes the Scanner object.
Program 2: Using for loop
In the below program we are using for loop to print frequency of digits of a given number in Java.
import java.util.Scanner;
public class FrequencyOfDigits {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int number = s.nextInt();
System.out.println("Digit \t Frequency");
int digit;
for (int i = 0; i <= 9; i++) {
int count = 0;
for (int num = number;num != 0;) {
digit = num % 10;
if (digit == i) {
count++;
}
num = num / 10;
}
if (count > 0) {
System.out.println(i + "\t" + count);
}
}
s.close();
}
}
Output:
Enter a number
1122
Digit Frequency
1 2
2 2
Java program explanation to print frequency of digits of a given number
- The program asks user for a number and stores it.
- Prints a header “Digit Frequency”.
- Loops through digits 0-9 (outer loop).
- Initializes a counter for digit frequency (inner loop).
- Iterates through the entered number digit by digit:
- Extracts the last digit.
- Checks if the extracted digit matches the current outer loop value.
- If they match, increments the counter for that digit.
- Removes the last digit for next iteration.
- Prints the digit and its frequency if it appeared at least once.
- Closes the Scanner object.
Conclusion
In this article, we have learnt how to print the frequency of digits of a given number. We have seen the program using while loop in Java and for loop in Java. Hope this article helped you in the understanding of the program.
You can also check our another awesome tutorials
- Java program to print all factors of a number
- Program to find strong numbers in a given range in Java
- Count the digits of a given number in java
- Java program to swap values without using third variable
Happy Programming!