Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Created February 2, 2018 23:11
Show Gist options
  • Save gitaficionado/49cb8e2b57493ca448d6e062df43f18e to your computer and use it in GitHub Desktop.
Save gitaficionado/49cb8e2b57493ca448d6e062df43f18e to your computer and use it in GitHub Desktop.
Write a program that reads the integers between 1 and 100 and counts the occurrences of each
/*
Count occurrence of numbers) 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:
Programming Exercises 277
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
Note that if a number occurs more than one time, the plural word “times” is used
in the output
*/
import java.util.Scanner;
public class CountOccuranceOfNumbers {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int[] counts = new int[100];
System.out.print("Enter the integers between 1 and 100: ");
// Read all numbers
int number = input.nextInt(); // number read from a file
while (number != 0) {
if (number <= ____ && number >= __)
counts[number - 1]++;
________ = input.nextInt();
}
// Display result
for (int ___ = 0; ____ < 100; i++) {
if (counts[i] > 0)
System.out.println((i + 1) + " occurs " + counts[i]
+ ((counts[i] == 1) ? " time" : " times"));
}
}
}
@hantheemp
Copy link

thanks, helped a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment