-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageValueOfElements.java
More file actions
39 lines (36 loc) · 914 Bytes
/
AverageValueOfElements.java
File metadata and controls
39 lines (36 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package array_Programming.medium.day_10;
//9. Calculate the average value of all elements in a given array.
import java.util.Arrays;
import java.util.Scanner;
public class AverageValueOfElements
{
public static int sum(int [] arr)
{
int sum = 0;
for(int i = 0;i<=arr.length-1;i++)
{
sum = sum + arr[i];
}
return sum;
}
public static void average(int [] arr)
{
int sum = sum(arr);
int average = sum/2;
System.out.println("The average of the elements present in the array is "+ average);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array : ");
int size = sc.nextInt();
int [] arr = new int[size];
System.out.println("Enter "+size+" elements:");
for(int i = 0;i<=arr.length-1;i++)
{
arr[i] = sc.nextInt();
}
System.out.println(Arrays.toString(arr));
average(arr);
sc.close();
}
}