ICSE Class 10 Computer Applications Question 9 of 59

Arrays — Question 10

Back to all questions
10
Question

Question 10

Suppose x is an array of type int[] with 50 elements. Write a code segment that will count and print the frequency of number 42 in the array.

Answer
int c = 0;
for (int i = 0; i < 50; i++) {
    if (x[i] == 42) {
        c++;
    }
}
System.out.println("Frequency of 42 = " + c);