-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathArraySumAverage.java
38 lines (30 loc) · 1.13 KB
/
ArraySumAverage.java
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
import java.util.Scanner;
public class ArraySumAverage {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Get the size of the array from the user
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
// Declare the array based on the user input
int[] numbers = new int[size];
// Get values for the array from the user
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}
// Calculate the sum of array elements
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
// Calculate the average of array elements
double average = (double) sum / size;
// Print the sum and average
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
// Close the Scanner
scanner.close();
}
}