Skip to content

Instantly share code, notes, and snippets.

@SlayterDev
Created July 8, 2014 14:37
Show Gist options
  • Save SlayterDev/5f2ea50d1e0514a1880b to your computer and use it in GitHub Desktop.
Save SlayterDev/5f2ea50d1e0514a1880b to your computer and use it in GitHub Desktop.
Calculate the average of a number of grades
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Brad's grade averager!!");
System.out.println("========================================");
System.out.println("Enter the number of grades to average: ");
int numGrades = input.nextInt();
// create array to hold grades
int[] grades = new int[numGrades];
// Fill the array from the console
for (int i = 0; i < numGrades; i++) {
System.out.println("Enter grade " + (i+1) + ": ");
grades[i] = input.nextInt();
}
// Calculate average
int sum = 0, average;
for (int i = 0; i < numGrades; i++) {
sum += grades[i];
}
average = sum / numGrades;
System.out.println("Your average is: " + average);
input.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment