# Java program to find sum & average of an array

Let’s learn java program to find sum & average of an array.

## Java program to find sum & average of an array

To find sum and average of an array first user enters (total) number of elements using nextInt() method of Scanner class.

This number is stored in integer variable “number”. Then this number is assigned to integer array “arrNum”.

Now user enters each elements or numbers which gets stored in integer array ‘arrNum’ and meanwhile calculate sum in for loop.

This sum is divided by total number of elements in array to calculate average.

Here’s the java program to find sum & average of an array or the java average of array and sum of an array.

```import java.util.Scanner;
public class SumAverageArrayDemo
{
public static void main(String[] args)
{
int number, sum = 0;
float average;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements : ");
number = sc.nextInt();
int[] arrNum = new int[number];
System.out.println("Enter " + number + " elements : ");
for(int a = 0; a < number ; a++)
{
arrNum[a] = sc.nextInt();
sum = sum + arrNum[a];
}
System.out.println("Sum is : " + sum);
// average of an array
average = (float)sum / number;
System.out.println("Average is : " + average);
sc.close();
}
}

```

Output:

Java program to calculate average of 3 numbers

To find average of 3 numbers in java we are taking input from user using nextDouble() method of Scanner class and storing in three variables.

These variables are then passed to calculateAverage() method to find average using “/” division operator.

Here’s java program to calculate average of 3 numbers.

```import java.util.Scanner;
public class AverageNumberDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double number1 = sc.nextDouble();
double number2 = sc.nextDouble();
double number3 = sc.nextDouble();
System.out.print("The average of 3 numbers is: " + calculateAverage(number1, number2, number3));
sc.close();
}
public static double calculateAverage(double num1, double num2, double num3)
{
return (num1 + num2 + num3) / 3;
}
}

```

Output:

The average of 3 numbers is: 379.0

### program to find average of n numbers

In program to find average of n numbers create variable num and set count equal to 1.

Next create variables ‘a’, average and total = 0 of type float.

In the while loop check count is less than or equal to num. Then total += a and count = count + 1.

Now set average = total / num. Lastly print average entered by user. Here’s program to find average of n numbers.

```import java.util.Scanner;
public class FindAverage
{
public static void main(String[] args)
{
int num, count = 1;
float a, average, total = 0;
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
while(count <= num)
{
System.out.println(count + " number: ");
a = sc.nextInt();
total += a;
++count;
}
average = total / num;
System.out.println("Average is: " + average);
sc.close();
}
}

```

Output:

1 number: 6
2 number: 9
3 number: 9
4 number: 9
5 number: 5
6 number: 9
Average is: 7.8333335

java program to calculate average marks

Here’s the java program to calculate average marks and percentage marks.

```import java.util.Scanner;
public class AverageMarksExample
{
public static void main(String[] args)
{
int[] marks = new int;
float total = 0;
float average, percentage;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter marks of 6 subjects: ");
for(int a = 0; a < 6; a++)
{
marks[a] = sc.nextInt();
total = total + marks[a];
}
average = total / 6;
percentage = (total / 600) * 100;
System.out.print("Average marks = " + average);
System.out.print("\nPercentage is = " + percentage + "%");
sc.close();
}
}

```

Output:

Please enter marks of 6 subjects: 91
92
96
90
98
94
Average marks = 93.5
Percentage is = 93.5%

This is all about java program to find sum & average of an array.

I have shown all examples with repect to sum and average of an array in java.

Above discussed java programs will be useful for freshers who are learning java programs for interview.