12. Program to print the sum of all the items of the array
💡Code:
public class SumOfArrayElements7078 {
public static void main(String[] args) {
// Example array
int[] numbers = {15, 7, 23, 45, 9, 56, 82, 31};
// Print the sum of all elements in the array
int sum = calculateSum(numbers);
System.out.println("Sum of all elements in the array: " + sum);
}
// Function to calculate the sum of all elements in an array
private static int calculateSum(int[] array) {
int sum = 0;
for (int num : array) {
sum += num;
}
return sum;
}
}
📸Output :