Java programs to calculate the sum and the average of N numbers have been shown here. The result can be obtained using an array or without an array. For example, if a set of given numbers is {10, 20, 30, 40, 50, 60}, the sum and average of these numbers are 210 and 35, respectively.

## 1. Algorithm to find the sum and average of N numbers

1. Take number of terms $n$ as inputs.

2. Set $s = 0$ and $i = 0$

3. Take a number $x$ as input.

4. Perform $s = s + x$ and $i = i + 1$

5. Check if $i < n$, then go to step 3, else go to step 6

6. Display $s$ as the sum and $\frac{s}{n}$ as the average and exit program.

## 2. Pseudocode to find the sum and average of N numbers

Input: Number of terms $n$

Output: Sum and average of those $n$ terms

1. Procedure sumAverage($n$):

2. $sum \leftarrow 0$

3. $i \leftarrow 0$

4. Repeat for each $i < n$:

5. Read a number $x$

6. $sum \leftarrow sum + x$

7. $i \leftarrow i + 1$

8. $avg \leftarrow \frac{sum}{n}$

9. Return $sum$, $avg$

10. End Procedure

## 3. Time complexity to find the sum and average of N numbers

Time Complexity: O(n)

Here $n$ is the number of terms.

## 4. Java Program & output to calculate sum & average of n numbers without an array

Code has been copied
/**************************************************************
alphabetacoder.com
Java program to calculate the Sum and the Average of N numbers
***************************************************************/

import java.util.Scanner;

class Main {
public static void main(String[] args) {
// declare instance of Scanner class
Scanner sc = new Scanner(System.in);
// declare variables
int i, n, num, sum = 0;
float avg;

// take input
System.out.print("Enter the no. of elements: ");
n = sc.nextInt();

// take input of numbers one by one
// add each number to sum
System.out.print("Enter " + n + " elements: \n");
for (i = 1; i <= n; i++) {
num = sc.nextInt();
sum = sum + num;
}

// calculate average
avg = (float) sum / n;

// display result
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
}
}


Output

Enter the no. of elements: 7

Enter 7 elements:

8 4 9 3 2 10 5

Sum: 41

Average: 5.857143

## 5. Java Program to calculate sum & average of n numbers using an array

Code has been copied
/****************************************
alphabetacoder.com
Java program to calculate the Sum and
the Average of N numbers using an array
*****************************************/

import java.util.Scanner;

class Main {
public static void main(String[] args) {
// declare instance of Scanner class
Scanner sc = new Scanner(System.in);
// declare variables
int i, n, sum = 0;
int[] N = new int[20];
float avg;

// take input
System.out.print("Enter the no. of elements: ");
n = sc.nextInt();

// take input of numbers one by one
// store the numbers in array N[]
System.out.print("Enter " + n + " elements: \n");
for (i = 0; i < n; i++) {
N[i] = sc.nextInt();
}

// calculate the sum of array elements
for (i = 0; i < n; i++) {
sum = sum + N[i];
}

// calculate average
avg = (float) sum / n;

// display result
System.out.println("Sum: " + sum);
System.out.println("Average: " + avg);
}
}


Output

Enter the no. of elements: 5

Enter 5 elements:

10 20 30 22 27

Sum: 109

Average: 21.8