Question: Write a program that generates 10 random integers between 0 and 9 and displays the count for each number.

Example:

3 4 4 5 0 3 5 2 0 6

0 appears 2 times.

1 appears 0 times.

2 appears 1 times.

3 appears 2 times.

4 appears 2 times.

5 appears 2 times.

6 appears 1 times.

7 appears 0 times.

8 appears 0 times.

9 appears 0 times.

Process finished with exit code 0

package Array;
/**
* Q: Write a program that generates 10 random
* integers between 0 and 9 and displays the count for each number.
* Created by aarushi on 13/5/21.
*/
public class Ch7Ex7 {
public static void main (String [] args){
//create array that stores the 100 randomly generated integers
int [] numbers= new int[10];
//create an array that stores the count of each number
int [] count= new int[10];
//generate and store the random numbers in numbers array
for (int i=0; i<numbers.length; i++){
numbers[i]= (int)(Math.random()*10);
}
//count the occurrence of each number
for(int i=0; i<numbers.length; i++){
count[numbers[i]]++;
}
//display array
for (int i=0; i<count.length; i++){
System.out.print(numbers[i]+ " ");
}
//display count
for (int i=0; i<count.length; i++){
System.out.print("\n" + i + " appears " + count[i] + " times.");
}
}
}

### Like this:

Like Loading...

*Related*