Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads integers, finds the largest of them, and counts its o

ID: 641941 • Letter: W

Question

Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4.

(Hint: Maintain two variables, Max and Count. Max stores the current maximum number, and Count stores the occurrences. Initially, assign the first number to Max and 1 to Count. Compare each subsequent number with Max. If the number is greater than Max, assign it to Max and reset Count to 1. If the number is equal to Max, increment Count by 1)

Explanation / Answer

ok I found an easy way out just to let you know:


import java.util.Scanner;

public class Occurances {
public static void main(String[] args) {
//Declare class variables
int count = 0;
int number = -1;
int max = -1;

//Create a scanner
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers (the input ends if it is 0): ");

//input user data but end when user enter 0
while (number != 0) {
number = input.nextInt();
//check if number is bigger than max
if (number > max) {
//if yes, set the number to max
max = number;
count = 1;
}
//if number is equal to max then increment count by 1
else if (number == max){
count++;
}
}

//Display max and occurrence
System.out.println("The largest number is: " +max);
System.out.println("The occurrence count of the largest number is " +count);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote