IN JAVA Write a program to read a list of exam scores given as integer percentag
ID: 3810314 • Letter: I
Question
IN JAVA
Write a program to read a list of exam scores given as integer percentages in the range 0 to 100. Display the total number of grades and the number of grades in each letter-grade category as follows: 90 to 100 is an A, 80 to 89 is a B, 70 to 79 is a C, 60 to 69 is a D, and 0
to 59 is an F.
Use a negative score as a sentinel value to indicate the end of the input. (The negative value is used only to end the loop, so do not use it in the calculations.) For example, if the input is
98 87 86 85 85 78 73 72 72 72 70 66 63 50 1
the output would be
Total number of grades = 14
Number of A’s = 1
Number of B’s = 4
Number of C’s = 6
Number of D’s = 2
Number of F’s = 1
Explanation / Answer
package gradecounter;
import java.util.Scanner;
public class GradeCounter {
public static void main(String[] args) {
Scanner gradeInp=new Scanner(System.in);
System.out.println("Enter the percentages: ");
int inputPercentage = 0; //store input integer
int totalGrades = 0; // store total number of grades
int totalAs = 0; // store A grade count
int totalBs = 0; // store B grade count
int totalCs = 0; // store C grade count
int totalDs = 0; // store D grade count
int totalFs = 0; // store F grade count
while(gradeInp.hasNextInt()){ //check if there is any integer left to read from input
inputPercentage = gradeInp.nextInt(); //store the input in temporary variable
if(inputPercentage >= 0 && inputPercentage <= 59){
totalFs = totalFs +1 ;
}
else if(inputPercentage >= 60 && inputPercentage <= 69){
totalDs = totalDs +1 ;
}
else if(inputPercentage >= 70 && inputPercentage <= 79){
totalCs = totalCs +1;
}
else if (inputPercentage >= 80 && inputPercentage <= 89){
totalBs = totalBs +1;
}
else if (inputPercentage >= 90 && inputPercentage <= 100){
totalAs = totalAs +1;
}
totalGrades= totalGrades +1;
}
System.out.println("Total number of grades = " + totalGrades);
System.out.println("Number of A's = " + totalAs);
System.out.println("Number of B's = " + totalBs);
System.out.println("Number of C's = " + totalCs);
System.out.println("Number of D's = " + totalDs);
System.out.println("Number of F's = " + totalFs);
}
}