166k views
2 votes
Calculate the average of a set of grades and count

the number of failing test grades

•Name your program lab8.c

•Start by asking the user how many grades are to be

entered.

•Then ask for each one of theses grades. Calculate the

average grade and the number of failing grades (grade

less than 70)

•Display on the screen the average grade and the

number of failing grades

1 Answer

7 votes

Answer:

// Scanner class is imported so that the program can can user input

import java.util.Scanner;

// class Solution is defined

public class Solution {

//main method which begin program execution is defined

public static void main(String args[]) {

// Scanner object scan is defined to receive input fromkeyboard

Scanner scan = new Scanner(System.in);

// A prompt is display to the user asking for number of grade

System.out.println("How many grade do you want to enter");

// The user input is assigned to numOfGrade

int numOfGrade = scan.nextInt();

// An array of integer called gradeArray is declared

// it has numOfGrade as length

int[] gradeArray = new int[numOfGrade];

// for-loop that prompt the user to enter grade to fill the gradeArray

for(int i = 0; i < numOfGrade; i++){

System.out.println("Enter a grade: ");

int grade = scan.nextInt();

gradeArray[i] = grade;

}

// sun variable is declared and initialized to 0

int sum = 0;

// numOfFail is declared and initialized to 0

int numOfFail = 0;

// for-loop through the gradeArray that add all element

// it also increase the counter for numOfFail if grade is < 70

for(int j = 0; j < gradeArray.length; j++){

sum += gradeArray[j];

if (gradeArray[j] < 70){

numOfFail++;

}

}

// aveargeGrade is calculated

int averageGrade = (sum / numOfGrade);

// numOfFail is displayed to the user

System.out.println("Number of fail is: " + numOfFail);

// the averageGrade is also displayed.

System.out.println("Average grade is: " + averageGrade);

}

}

Step-by-step explanation:

The code is well commented and written in Java.

User Darkbound
by
4.3k points