Home Java Program Examples Java Array Program to find average

Java Array Program to find average

by anupmaurya

In this Example you will learn how to find average of temperatures using java array.

Suppose you want to examine a series of high temperatures, compute the average temperature, and count how many days were above average in temperature.

// Reads a series of high temperatures and reports the average.

import java.util.*;
public class Temperature1 {
         public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("How many days' temperatures? ");
        int numDays = console.nextInt();
        int sum = 0;
        for (int i = 1; i <= numDays; i++) {
        System.out.print("Day " + i + "'s high temp: ");
           int next = console.nextInt();
           sum += next;
       }
      double average = (double) sum / numDays;
        System.out.println();
      System.out.println("Average = " + average);
      }
 }

OUTPUT

This program does a pretty good job. Here is a sample execution:

How many days' temperatures? 5
Day 1's high temp: 78
Day 2's high temp: 81
Day 3's high temp: 75
Day 4's high temp: 79
Day 5's high temp: 71

Average = 76.8

But how do you count how many days were above average?

This can be done using Looping Concepts ,Let have a look how the loop look like,

int above = 0;
for (int i = 0; i < temps.length; i++) {
if (temps[i] > average) {
above++;
}
}

You are seen in above example .we are using looping with comparison to find the how many days were above average.

Now Let’s have an look on complete program.

Example: Reads a series of high temperatures and reports the average using Java array

// Reads a series of high temperatures and reports the average.

import java.util.*;
public class Temperature1 {
         public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("How many days' temperatures? ");
        int numDays = console.nextInt();
       int[] temps = new int[numDays];
       
        // record temperatures and find average
        int sum = 0;
        for (int i = 1; i <= numDays; i++) {
        System.out.print("Day " + i + "'s high temp: ");
           int next = console.nextInt();
           sum += next;
       }
    
         double average = (double) sum / numDays;
      // count days above average
          int above = 0;
         for (int i = 0; i < temps.length; i++) {
                 if (temps[i] > average) {
                              above++;
                   }
      }
        System.out.println();
        System.out.println("Average = " + average);
        System.out.println(above + " days above average");

      }
 }

OUTPUT

Here is a sample execution:

How many days' temperatures? 9
Day 1's high temp: 75
Day 2's high temp: 78
Day 3's high temp: 85
Day 4's high temp: 71
Day 5's high temp: 69
Day 6's high temp: 82
Day 7's high temp: 74
Day 8's high temp: 80
Day 9's high temp: 87
Average = 77.88888888888889
5 days above average

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.