34.8k views
4 votes
Write a Java program to generate 200 random integers in the range of 0 and 999 (both inclusive). Then find the appearance frequency of each digit (0-9) in these numbers, and print a frequency histogram.

User Ranfis
by
5.5k points

1 Answer

3 votes

Answer:

import java.util.Random;

public class ArrayBar {

public static void main(String[] args) {

int arr[] = new int[10];

Random r = new Random();

int n = 0;

for (int i = 0; i < 200; i++) {

n = r.nextInt(1000);

while (n > 0) {

arr[n % 10]++;

n = n / 10;

}

}

for (int i = 0; i < 10; i++) {

System.out.print(i + " (" + arr[i] + "): ");

for (int j = 0; j < arr[i]; j++) {

System.out.print("*");

}

System.out.println();

}

}

}

Step-by-step explanation:

User Bertram Gilfoyle
by
5.3k points