Digit Frequency in Java: Explanation & Practice
Count occurrences of a digit in a number
Problem summary
Count how many times a specific digit appears in a number.
Starter code
public class Main {
public static void main(String[] args) {
int number = 122333;
int digit = 3; // Test case 1
// Count occurrences of digit in number
// Print: Count: <result>
}
}Expected output and test cases
- 3 appears 3 times in 122333
Count: 3
- 2 appears 2 times in 122333
Count: 2
- 5 appears 0 times in 122333
Count: 0
Hints
- Extract each digit using modulus
- Compare with target digit
- Count matches while reducing number
Related Control Flow exercises
Practice all Control Flow exercises · Run this idea in the Java compiler