public class ArraySumAverage { public static void main(String[] args) { // Predefined array of integers int[] array = { 10, 20, 30, 40, 50 }; // Calculate sum and average int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } double average = (double) sum / array.length; // Output: Sum and Average System.out.println("Sum of the elements: " + sum); System.out.println("Average of the elements: " + average); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter