Answer:
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args)
- {
- int numArr [] = new int[51];
- int num;
- Scanner input = new Scanner(System.in);
-
- do{
- System.out.print("Input number between 0 - 50: ");
- num = input.nextInt();
- if(num >= 0 && num <= 50){
- numArr[num]++;
- }
- }while(num != -1);
-
- for(int i=0; i < numArr.length; i++){
- if(numArr[i] > 0){
- System.out.println(numArr[i] + " occurrences of " + i);
- }
- }
- }
- }
Step-by-step explanation:
The solution code is written in Java.
Firstly, create a numArr array with 51 elements (Line 6).
Next, use do while loop to repeatedly prompt user for an input number between 0 - 50. If num is between 0 - 50, use the input number as index to increment that particular element of array by one (Line 10 - 15).
At last, create another for-loop to traverse through the numArr array and print all the elements with value at least one (Line 18 - 21).