Java – Calculate Average of Numbers
To calculate the average of numbers in an array or arraylist, use a loop statement to find the sum of all the numbers in the array, and divide the sum with number of elements.
In this tutorial, we write Java programs to compute average of numbers in an array and ArrayList.
Example 1 – Average of Numbers in Array
In this example, we shall use Java While Loop, to compute the average.
Algorithm
We shall use the following algorithm to find the average of numbers.
- Start.
- Read array of numbers. Or initialize an array with number, of whom you would like to find average.
- Initialize sum = 0;
- Initialize i = 0;
- Check if i is less than number of elements in array. If not go to step 8.
- Add sum with number in array at index i, and store in the sum itself.
- Increment i. Go to step 5.
- Compute average = sum / number of elements in array.
- Stop.
Java Program
/**
* Java Program - Average of Numbers
*/
public class Average {
public static void main(String[] args) {
//numbers
int[] nums = {1, 2, 3, 4, 5, 6};
float sum = 0;
//compute sum
int i=0;
while(i < nums.length) {
sum += nums[i];
i++;
}
//compute average
float average = (sum / nums.length);
System.out.println("Average : "+average);
}
}
Note: We are using float datatype for sum
variable to save the precision after computing average. If you use int datatype for sum
, when dividing it with number of elements, we lose the decimal part of the average.
Output
Average : 3.5
Example 2 – Average of Numbers in Array
In this example, we shall use Java Advanced For Loop, to compute the average.
Algorithm
We shall use the following algorithm to find the average of numbers.
- Start.
- Read array of numbers. Or initialize an array with numbers, of whom you would like to find average.
- Initialize sum = 0;
- For each number in the array, add the number to sum.
- Compute average = sum / number of elements in array.
- Stop.
Java Program
/**
* Java Program - Average of Numbers
*/
public class Average {
public static void main(String[] args) {
//numbers
int[] nums = {1, 2, 3, 4, 5, 6};
float sum = 0;
//compute sum
for(int num:nums)
sum += num;
//compute average
float average = (sum / nums.length);
System.out.println("Average : "+average);
}
}
Output
Average : 3.5
Example 3 – Average of Numbers in ArrayList
In this example, we shall use Java Advanced For Loop, to compute the average.
Algorithm
We shall use the following algorithm to find the average of numbers.
- Start.
- Read array of numbers. Or initialize an ArrayList with numbers, of whom you would like to find average.
- Initialize sum = 0;
- For each number in the ArrayList, add the number to sum.
- Compute average = sum / number of elements in array.
- Stop.
Java Program
import java.util.ArrayList;
/**
* Java Program - Average of Numbers
*/
public class Average {
public static void main(String[] args) {
//numbers
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(10);
nums.add(13);
float sum = 0;
//compute sum
for(int num:nums) {
sum += num;
}
//compute average
float average = (sum / nums.size());
System.out.println("Average : "+average);
}
}
Output
Average : 11.5
Conclusion
In this Java Tutorial, we learned how to find the average of numbers in an array or a ArrayList, using looping statements.