Challenge: Class Report
Completed
Read the marks of n students and print three lines: the lowest mark, the highest mark and the average with exactly 2 decimal places. Do it in a single pass over the marks.
Input
Line 1: n (n ≥ 1). Line 2: n whole numbers from 0 to 100.
Output
Lowest: <x>, Highest: <y>, Average: <z> on three lines.
Example
Input
5 70 85 92 64 88
Output
Lowest: 64 Highest: 92 Average: 79.80
Editor
Java
Hints
Start low above any real mark (101) and high below any (-1).
String.format("%.2f", x) gives exactly 2 decimals. Divide as double, not int.
Solution explanation
One loop keeps three running values at once: O(n) time, O(1) memory. Casting to double before dividing stops Java from throwing away the decimals.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int low = 101, high = -1;
long total = 0;
for (int i = 0; i < n; i++) {
int m = sc.nextInt();
if (m < low) low = m;
if (m > high) high = m;
total += m;
}
System.out.println("Lowest: " + low);
System.out.println("Highest: " + high);
System.out.println("Average: " + String.format("%.2f", (double) total / n));
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module