Hello everyone!
Once again I need some help on this program. I need to write a program that reads integers, finds the largest of them, and counts how many times that number occurs. The program should end when 0 is input. My teacher gave us a hint, incase this helps "Maintain two variables, max and count. max stores the current max number, and count stores its occurrences. Initially, assign the first number to max and count to 1. 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."

Sample 1:
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5
The occurrence count of the largest number 4

Ok, so heres what I have so far.

``````import java.util.Scanner;
public class occurence
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int max;
int num=0;
int counter=1;
max = input.nextInt();

do
{
num = input.nextInt();
}while(num != 0);

int largest = max(num);

if(max<num){
counter=1;
}
else if(max==num){
counter++;
}

System.out.println("there are " + counter + " of the largest number");

}
}``````

I have no idea what im doing hahaha. However I guess my main problem is how to compare max to the next subsequent numbers.

## All 5 Replies

what is it doing wrong?
as far as I see from your code, what you're doing wrong is that you don't compare max and adding to number times occurences within your loop.

I'm actually unsure of how to compare max.

You could store the numbers in an array of integers then compare them to know which is the highest number

why so difficult?
PSEUDO:

``````store the first number in a variable maxNumber
While ( entering numbers ){
if ( enteredNumber greater than maxNumber ){
maxNumber = enteredNumber
}
}``````

I change my mind stultuske's method is more efficient

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.