Created
February 2, 2018 23:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey this my source code for it. I know its not perfect but it work perfectly.
package test2;
import java.util.Arrays;
import java.util.Scanner;
public class Test2 {
for(int i = 0; i< (count-1); i++) { //get the array until 0th number into new
mySort[i] = myArray[i];
}
}