-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumAndAverageOfArray.java
More file actions
32 lines (28 loc) · 1.07 KB
/
SumAndAverageOfArray.java
File metadata and controls
32 lines (28 loc) · 1.07 KB
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
import java.util.Scanner;
public class SumAndAverageOfArray {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Sum and Average of the given array");
System.out.println("Enter the array index number");
int n= input.nextInt();
if (n<=0){
System.out.println("The number of elements should be greater than 0.");
return;
}
int[] arr;
arr = new int[n];
System.out.println("Enter the Elements in the array");
for (int i = 0; i < arr.length; i++) {
arr[i] = input.nextInt();
}
System.out.println("Calculate the sum of the elements in the array");
int sum=0;
for (int num : arr) {
sum += num;
}
System.out.println("The sum of all the elements in the array is "+sum);
System.out.println("Calculate the Average");
int average=sum/n;
System.out.println("The Average of the elements in the array is "+average);
}
}