In this post, we will write a two java program to Find Sum and **Average **of N numbers using an array. The first program finds the average of specified array elements. The second program takes the value of N and the numbers provided by the user and finds the average of them using an array.

To understand these programs you should have knowledge of following concepts:

- Java Arrays
- For loop

**How the program will work?**

- First of all, accept an array.
- Now using loop we calculate the sum of elements of the array.
- Hence we have sum the divide it by array count to get average.

**Example:**

- Input:
- Enter the array size: 5
- Enter the 5 array elements
- 6
- 4
- 5
- 8
- 6
**Output:**- Sum of 5 number is 29.
- Average is 5.8.

**Example 1:** **Program to Calculate average using Array **

In this program, we have a predefined value of array which will get add using** for loop** and then calculate the average by dividing the sum by count of array count.

public class demo { public static void main(String[] args) { int arr[] = {5,6,48,6,78,20,65}; int len = arr.length; double avg =0; double sum = 0; for (int i=0;i<len;i++){ sum += arr[i]; } avg = sum/len; System.out.println("Sum of "+len+" number in "+sum); System.out.println("Average is "+avg); } }

**Output:**

Sum of 7 number in 228.0 Average is 32.57

**Example 2:** **Program to Calculate the average of numbers entered by the user**

In this program, we are using a Scanner to get the value of n and all the numbers from the user. First of all, take the array size for the user and then accept the array value using a loop. After that calculate the Sum of the array element using second for loop and divide it by array size to get the average of an array.

import java.util.Scanner; public class sum_of_array { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the array size: "); int len = sc.nextInt(); int arr[] = new int[len]; double sum = 0; double avg = 0; System.out.println("Enter the 5 array element"); for (int i=0;i<len;i++){ arr[i] = sc.nextInt(); } for (int i=0;i<len;i++){ sum += arr[i]; } avg = sum/len; System.out.println("Sum of "+len+" number in "+sum); System.out.println("Average is "+avg); } }

**Output:**

Enter the array size: 5 Enter the 5 array element 45 48 65 35 48 Sum of 5 number in 241.0 Average is 48.2

**Recommended:**

- Java Program to solve quadratic equations
- Java Program to find the greatest of three Numbers
- Java Program to Display the weekday between 1 and 7
- Java Program to Find the number of days in a Months.
- Java Program to Print character is vowel or Consonant
- Java Program to Check a year is a leap year or not
- Java Program to display the sum of first N natural numbers