Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that generates 100 random integers between 0 and 9 and displays

ID: 3691646 • Letter: W

Question

Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.)

public static void main(String[] args) {

int[] counts = new int[10];

for (int i = 0; i < 100; i++) {
int num = getRandomInt(0, 9);
counts[num]++;
}
for (int i = 0; i < counts.length; i++) {
System.out.println("The number " + i + " occurs " + counts[i] + " times.");
}
}

public static int getRandomInt(int lowerBound, int upperBound) {
int range = (upperBound - lowerBound) + 1;
return (int) (Math.random() * range) + lowerBound;
}
}

Explanation / Answer

public class randomAndOccurance {

public static void main(String args[]) {

int [] num=new int[10];

int j;

for(int i=0; i<num.length; i++)

{

num[i]=(int) (Math.random()*10);

j=j+1;

System.out.println("Count for" + (j) + " is " +num[i]);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote