In this post, we will learn **to code the Java Program to Calculate Average of N Numbers.** Let’s understand **“How to find the average of n given numbers”**.

**What is Average ?**

An **Average **is a number taken as representative of the list of numbers, which is calculated by **adding all the numbers divided by the count of the total numbers added**.

**For Example :****Average of numbers 5, 6, 9, 10 will be calculated as follows:**

**Average = (5 + 6 + 9 + 10) / 4** **= 30/4 = 7.5**

**Hence, 7.5 is the average of the given list i.e. 5, 6, 9 & 10**

Now, we have learned the formula to calculate the average of n numbers. Let’s see the **Java Program to Calculate Average of N Numbers. **We are going to solve it using two methods, **direct** & **using arrays.**

**Java Program to Calculate Average of N Numbers**

**Method 1: Direct **

import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Total count of number to find the average of: "); int n = sc.nextInt(); int sum = 0; System.out.println("Enter the numbers: "); for(int i=0;i<n;i++){ sum += sc.nextInt(); } double average = sum/(double)n; // typecasting n from int to double for decimal division System.out.println("Average of n given numbers is "+average); } }

**Output**

`Total count of number to find the average of: `**10**
Enter the numbers: **1 2 3 4 5 6 7 8 9 12**
**Average of n given numbers is 5.7**

**How Does This Program Work ?**

Scanner sc = new Scanner(System.in); System.out.println("Total count of number to find the average of: "); int n = sc.nextInt();

In this program, we have taken the input of the total count of numbers to find the average and stored the value in the variable named** “n”** using the Scanner class in Java.

int sum = 0; System.out.println("Enter the numbers: "); for(int i=0;i<n;i++){ sum += sc.nextInt(); }

Then, we declared a variable sum in which we are directly taking the input and adding it by using **sum += sc.nextInt()**

double average = sum/(double)n; // typecasting n from int to double for decimal division System.out.println("Average of n given numbers is "+average);

Then, we calculate the **average** by dividing the **sum** by **n**, we also typecasted the type of **n **from **integer to double datatype** to perform **decimal division**, and then the calculated value is stored in the variable **average** of** double datatype. **

Then, we print the average we calculated. This is the **Java Program to Calculate Average of N Numbers.**

Now, let’s see another way of **calculating the average of n numbers in java by using arrays.**

**Java Program to Calculate Average of N Numbers Using Arrays.**

import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Total count of number to find the average of: "); int n = sc.nextInt(); int[] arr = new int[n]; System.out.println("Enter the numbers: "); for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } int sum=0; for(int val : arr){ sum += val; } double average = sum/(double)n; // typecasting n from int to double for decimal division System.out.println("Average of n given numbers is "+average); } }

**Output**

`Total count of number to find the average of: `**1**1
Enter the numbers: **1 2 3 4 5 6 7 8 9 10 11**
**Average of n given numbers is 6.0**

**How Does This Program Work ?**

In this program, we created an **array** of size **n** and stored the numbers in the array.

int sum=0; for(int val : arr){ sum += val; }

Then, we initialized a sum variable to calculate the sum of all the elements of the array.

Then, using the **for-each loop in Java**, we traversed through all the elements of the number and added them.

double average = sum/(double)n; // typecasting n from int to double for decimal division System.out.println("Average of n given numbers is "+average);

Then, we calculate the **average** by dividing the **sum** by **n**, we also typecasted the type of **n **from **integer to double datatype** to perform **decimal division**, and then the calculated value is stored in the variable **average** of** double datatype. **

Then, we print the average we calculated. This is the **Java Program to Calculate Average of N Numbers Using Arrays.**

**Some Example Programs**

**Java Program to Calculate the Average of 5 Numbers**

import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int sum=0; System.out.println("Enter 5 numbers: "); for(int i=0;i<5;i++){ sum += sc.nextInt(); } double average = sum/5.0; System.out.println("Average of 5 numbers is "+average); } }

**Output**

`Enter 5 numbers: `**2 3 4 5 6**
**Average of 5 numbers is 4.0**

**Java Program to Calculate Average of 3 Numbers**

import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter 1st number: "); int n1 = sc.nextInt(); System.out.println("Enter 2nd number: "); int n2 = sc.nextInt(); System.out.println("Enter 3rd number: "); int n3 = sc.nextInt(); double average = (n1+n2+n3)/3.0; System.out.println("Average of given 3 numbers is "+average); } }

**Output**

`Enter 1st number: `**10**
Enter 2nd number: **12**
Enter 3rd number: **14**
**Average of given 3 numbers is 12.0**

In this question, the average of 3 numbers can also be calculated using the methods above explained i.e. **Java Program to Calculate Average of N Numbers, **where **n = 3**.

This was the **Java Program to Calculate Average of N Numbers.**

**Conclusion**

I hope after going through this post, you understand how to code **Java Program to Calculate Average of N Numbers. **We also discussed the Java Program to Calculate Average of N Numbers using Arrays. We also explain two examples i.e. **Java Program to Calculate the Average of 5 Numbers & the Java Program to Calculate the Average of 3 Numbers.**

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

**Also Read:**

- Java Program to Add Two Complex Numbers
**Java Program to Multiply Two Numbers****Java Program to Swap Two Numbers****Java Program to Find Factorial of a Number****Java Program to Check Prime Number**