Write a java program that accepts given n number of marks for a PRG510S test, and stores them into an array named marks. After all marks have been entered your program should accomplish the following:
[n - to be provided by user input]
a) Find and display the highest mark
b) Find and display the lowest mark
c) Compute and display the average mark
(Above tasks (a, b, and c should be accomplished using only one loop)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int number;
System.out.print("Enter numbers of marks:");
number=input.nextInt();
int [] marks = new int [number];
System.out.print("Enter "+number+" marks:");
for(int x=0;x<number;x++){
marks[x]=input.nextInt();
}
int High=marks[0];
int low=marks[0];
int Ave;
int add=0;
for(int x=0;x<number;x++){
add+=marks[x];
if(marks[x]>High){
High=marks[x];
}
if(marks[x]<low){
low=marks[x];
}
}
Ave=add/number;
System.out.println("Highest Mark = "+High+"%");
System.out.println("Lowest Mark = "+low+"%");
System.out.println("Average = "+Ave+"%");
}
}
Comments
Leave a comment