In: Computer Science
Write a java program that takes 5 students final test scores from console when prompting. Store these scores in an array. Iterate thru the array of student scores to find –
Highest score entered
Lowest score entered
Average of all the 5 scores
Total of all the scores
Print these values to the console (screen).
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int marks[];
marks = new int[5];
//taking input from users
for(int i=0;i<5;i++) {
System.out.print("Enter Student " +
(i+1) + " score: ");
marks[i] = scan.nextInt();
}
//Initiating high, low to marks 0th index and sum, average to
0
int high = marks[0];
int low = marks[0];
int sum = 0;
int average = 0;
//calculating highest, lowest and sum of scores
for(int i=0;i<5;i++) {
if (marks[i] > high)
high = marks[i];
if (marks[i] < low)
low = marks[i];
sum = sum + marks[i];
}
average = sum/5;
//displaying result
System.out.println("\nThe highest score is: " + high);
System.out.println("The lowest score is: " + low);
System.out.println("The sum of all scores is: " + sum);
System.out.println("The average of all score is: " +
average);
}
}
Code Photo:
Output:
