Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Exercise_05_41.java #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 36 additions & 37 deletions Exercise_05/Exercise_05_41/Exercise_05_41.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
/*
(Occurrence of max numbers) 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 5 0; the program finds that the largest
is 5 and the occurrence count for 5 is 4.
/* 4. 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 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 max number,
and count stores its 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.)
and count stores its 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.)
*/
import java.util.Scanner;

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

// Prompt the user to enter number
System.out.print("Enter numbers: ");
int max = input.nextInt(); // Assign the first number to max
int count = 1; // Assign 1 to count
int number; // Holds future inputs

// Assume that the input ends with number 0
while (number > 0) {
number = input.nextInt();
if (number > max) {
max = number;
count = 1;
}
if (number == max)
count++;
}

// Display to results
System.out.println("The largest number is " + max);
System.out.println(
"The occurrence count of the largest number is " + count);
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Create Scanner

//Prompt the user to enter a series of numbers
System.out.print("Enter a series of numbers: ");
int max = input.nextInt(); //Assign the numbers as max
int count = 0; //Initialize count as 0
int number; //Placeholder for the other numbers

do {
number = input.nextInt();
//Find max
if (number > max) {
max = number;
}
//Count occurrence for max number
if (number == max)
count++;
} while (number > 0);

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