HW Help? I cannot seem to figure this one out..
--Write a program that reads the integers between 1 and 100 and counts the occurrences of each. assume the input ends with 0.
Example: 2 5 6 5 4 3 23 43 2 0 ENTER
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.--
Java Code for the given question:
import java.util.Scanner;
public class Base1 {
public static void main(String[] args) {
//Scanner class to get the input from user
Scanner scan = new Scanner(System.in);
System.out.println("Enter the integers between 1 to 100 and end with 0");
String input = scan.nextLine();
String[] integerStrings = input.split(" ");
int[] integers = new int[integerStrings.length];
// Creates the integer array.
for (int i = 0; i < integers.length; i++){
integers[i] = Integer.parseInt(integerStrings[i]);
//Parses the integer for each string.
}
//Iterating over 1 to 100 to find the number of times each number occured
for (int i = 1; i<=100; i++){
int count =0;
for(int j:integers){
if (i==j){
count++;
}
}
if (count==1){
System.out.println(i+" occurs "+count+" time");
}
else if(count>1){
System.out.println(i+" occurs "+count+" times");
}
}
//Closing the scanner function
scan.close();
}
}
Output:
HW Help? I cannot seem to figure this one out.. --Write a program that reads the...
(Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and counts the occurrences of each. Here is a sample run of the program: Enter integers between 1 and 100: 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 2 5 6 5 4 3 23 43 2 Note that if a number occurs more than...
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 02 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times6 occurs 1 time23 occurs 1 time43 occurs 1 timeNote that if a number occurs more than one time, the plural word...
Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header: public static String convertMillis(long millis) For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...
Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...
Could someone please help me with the following;Write a program that reads the integers between 1 and 100 and counts the occurences of each. Assume the input ends with 0. You should have two additional methodsbesides main. Their signature is:public static void displayResults(int[] numList) - This method displays the results as shown below.public static int getValidInput() - This method gets the input from the keyboard, makes sure it falls between 1 and 100 and if so returns the number to...
C Program Question: Write a program that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file. Note: The program ignores any number less than 0 or greater than 100. Note: Do not display zero if a...
Write a program that reads integers, finds the smallest of them, and counts its occurrences. Assume that the input ends with number -1. Suppose that you entered 4 2 9 2 2 -1; the program finds that the smallest is 2 and the occurrence count for 2 is 3. (Hint: Maintain two variables, min and count. min stores the current min number, and count stores its occurrences. Initially, assign the first number to min and 1 to count. Compare each...
write a program that read the integers between 1 and 100 and count the occurrence of each number. Assume the input end with 0. here is samole of the program:Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0 Enter2 occurs 2 times3 occurs 1 time4 occurs 1 time5 occurs 2 times6 occurs 1 time23 occurs 1 time43 occurs 1 time
USING PYTHON: Write a program that reads an unspecified number of integers and finds the ones that have the most occurrences. For example, if you enter 2 3 40 3 5 4 –3 3 3 2 0, the number 3 occurs most often. Enter all numbers in one line. If not one but several numbers have the most occurrences, all of them should be reported. For example, since 9 and 3 appear twice in the list 9 30 3 9...
Do pseudocode in java Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and number of times it appeared as shown...