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"));
}
}
}
@samal36
Copy link

samal36 commented Apr 1, 2020

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 {

public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);

// ask for user to input numbers
System.out.println("Enter some integers between 1 and 100 (and 0 when done): ");

int[] myArray = new int[1000];//create a new array for user inputs

int number;//variable for user inputs
int count = 0;

do
{
    number = input.nextInt();
    myArray[count] = number;
    count++;
}

while (number != 0); 
int[] mySort = new int [count - 1]; //create a new array with only the numbers 

for(int i = 0; i< (count-1); i++) { //get the array until 0th number into new
mySort[i] = myArray[i];
}

java.util.Arrays.sort(mySort);// sort the array in ascending order

int n = 0;

for(int i = 0; i < mySort.length; i++) {//check if the number have checked before
	int occurance = 0;
	if(n >= mySort[i]) {
		continue;
	}
	else {
	n = mySort[i];//if a new number found do the calculation+ 
	for (int j=0; j<mySort.length; j++) 
        if (n == mySort[j]) 
          occurance++; 
	System.out.print(n +  " occurs " + occurance );
	{
		if (occurance == 1) {
		System.out.println(" time.");
		}
		else {
			System.out.println(" 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