In this example, we will see “How to calculate average of N numbers in Java 8?”. To achieve that, we are going to use Stream API introduced in Java 8 to find average and we will see it through several different ways.

Java 8 programs to find average of n numbers...!!! Click To Tweet

In this tutorial, we are going to cover below topics:

  1. How to calculate average of N numbers?
  2. Different ways of calculating the average.

Let’s begin:

1. How to calculate average of N numbers?

It’s very easy to find out the average. Average has been calculated as sum of n numbers divided by n.

In order to calculate average, add all the numbers and divide them by count of numbers

Let’s take an example, 

There are following numbers available: 7, 10, 81, 33, 69 

And, we need to find the average of these numbers, what we need to do is:

a. First add all the numbers: (7+10+81+33+69) =  200

b. Count the numbers: (7,10,81,33,69) = 5

c. Find average by dividing a from b: 200 / 5 = 40

2. Different ways of calculating the average.

Example 1. Java 8 code to find average of numbers using List

Output:

Example 2. Java 8 code to find average of numbers using Array

Output:

Example 3. Java 8 code to find average of numbers using Array with Scanner

Output:

Java 8 programs to find average of n numbers...!!! Click To Tweet

Do you like this Post? – then check my other helpful posts:

Other Useful References: