-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path064-Average-of-Array-Elements.java
More file actions
110 lines (82 loc) · 3.3 KB
/
Copy path064-Average-of-Array-Elements.java
File metadata and controls
110 lines (82 loc) · 3.3 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* ============================================================================
* Program Name : Average of Array Elements
* File Name : 064-Average-of-Array-Elements.java
* Class Name : AverageOfArrayElements
*
* Description:
* This program accepts the size and elements of a one-dimensional array
* from the user and calculates the average of all array elements.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to traverse a one-dimensional array.
* - Calculate the average of array elements using loops.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import java.util.Scanner;
public class AverageOfArrayElements {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard.
Scanner scanner = new Scanner(System.in);
// Ask the user to enter the size of the array.
System.out.print("Enter the Size of the Array: ");
// Read the size entered by the user.
int size = scanner.nextInt();
// Check whether the entered size is valid.
if (size <= 0) {
// Display an error message.
System.out.println("Please enter a positive array size.");
} else {
// Declare a one-dimensional integer array.
int[] array = new int[size];
// Declare and initialize the sum variable.
int sum = 0;
// Declare the average variable.
double average;
// Ask the user to enter the array elements.
System.out.println("Enter " + size + " Array Elements:");
// Read the array elements and calculate the sum.
for (int i = 0; i < size; i++) {
// Read the current array element.
array[i] = scanner.nextInt();
// Add the current element to the sum.
sum = sum + array[i];
}
// Calculate the average of the array elements.
average = (double) sum / size;
// Display the array elements.
System.out.println("Array Elements:");
// Traverse the array and print each element.
for (int i = 0; i < size; i++) {
// Print the current array element.
System.out.print(array[i] + " ");
}
// Move the cursor to the next line.
System.out.println();
// Display the sum of the array elements.
System.out.println("Sum of Array Elements: " + sum);
// Display the average of the array elements.
System.out.println("Average of Array Elements: " + average);
// Example Output:
// Enter the Size of the Array: 5
// Enter 5 Array Elements:
// 10
// 20
// 30
// 40
// 50
// Array Elements:
// 10 20 30 40 50
// Sum of Array Elements: 150
// Average of Array Elements: 30.0
}
// Close the Scanner object to release system resources.
scanner.close();
}
}