In: Computer Science
Write a program in Java that asks users to enter a series of marks (one at a time) until a signal is entered to stop. Have your program ensure that each mark entered is between 0 and 100. When done, output the number of marks entered and the average of all marks. Also output the highest mark and the lowest mark, and the range between the highest and lowest mark.
The signal considered to stop is -1.
JAVA Program:
import java.io.*;
import java.util.*;
class MarksCounter {
public static void main (String[] args) {
int
count=0,totalmarks=0,high=-1,low=101,temp;
float average;
Scanner sc = new
Scanner(System.in);
while(true){
System.out.print("Enter a mark
between 0 and 100(-1 to exit): ");
temp = sc.nextInt();
if(temp==-1)
break;
if(temp<0||temp>100){
System.out.println("Invalid
mark.");
continue;
}
totalmarks += temp;
count++;
if(temp>high)
high = temp;
if(temp<low)
low = temp;
}
System.out.println("Number of valid
marks entered is "+count);
System.out.printf("Average of valid
marks is %.2f\n",(float)totalmarks/(float)count);
System.out.println("Highest valid
mark is "+high);
System.out.println("Lowest valid
mark is "+low);
System.out.println("Range between
highest and lowest valid mark is "+(high-low));
}
}
Output: