1.Marks statistics
BeginnerGiven int[] marks = {56, 91, 78, 34, 88};, print the total, the average and the highest mark.
Show hint
Loop once, adding to a total and tracking the highest so far. Cast before dividing for the average.
Show solution
public class Main {
public static void main(String[] args) {
int[] marks = {56, 91, 78, 34, 88};
int total = 0;
int highest = marks[0];
for (int m : marks) {
total += m;
if (m > highest) highest = m;
}
System.out.println("Total: " + total);
System.out.println("Average: " + (double) total / marks.length);
System.out.println("Highest: " + highest);
}
}Total: 347
Average: 69.4
Highest: 91