Program to count frequency of a digit using Java | Java Programming | AskTheCode
Problem:
Count the frequency of a particular digit in a given number.
Input sample:
3 983653
Output Sample:
2
Code:
import java.util.Scanner;
public class freq_of_a_digit{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int digit = scan.nextInt();
int n = scan.nextInt();
int[] arr = new int[20];
int i = 0, freq = 0;
while(n != 0){
int val = n % 10;
arr[i] = val;
i++;
n = n / 10;
}
for (int j = 0; j <= i; j++) {
if (arr[j] == digit) {
freq += 1;
}
}
System.out.println(freq);
}
}
Commentaires