Question: Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is
Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program: Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time
This is my current code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numbers = new int[100];
System.out.print("Enter the integars between 1 and 100: "); for (int i = 0; i < 100; i++) { int n = input.nextInt(); numbers[n] += n; if (n == 0) { break; } }
for (int i = 1; i < numbers.length; i++) { if (numbers[i] != 0) { if (numbers[i] / i > 1) { System.out.println(i + " occurs " + numbers[i] / i + " times "); } } else { System.out.println(i + " occurs " + numbers[i] / i + " time "); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
