-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestSmallestAverage.java
More file actions
42 lines (32 loc) · 1.19 KB
/
LargestSmallestAverage.java
File metadata and controls
42 lines (32 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner;
public class LargestSmallestAverage {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
int count = 0;
int sum = 0;
int largest = Integer.MIN_VALUE; // Initialize with minimum possible integer
int smallest = Integer.MAX_VALUE; // Initialize with maximum possible integer
System.out.println("Enter numbers (enter 0 to stop):");
do {
number = scanner.nextInt();
count++;
if (number > largest) {
largest = number;
}
if (number < smallest) {
smallest = number;
}
sum += number;
} while (number != 0);
scanner.close();
if (count > 0) { // Check if any number was entered
double average = (double) sum / count;
System.out.println("Largest number: " + largest);
System.out.println("Smallest number: " + smallest);
System.out.printf("Average of %.0f numbers: %.2f\n", (double) count, average);
} else {
System.out.println("No numbers were entered.");
}
}
}