Generation

code to explanationSun, 05 Feb 2023

e import java.util.Arrays; import java.util.Scanner; public class SumOfElementsOfAnArray { public static void main(String args[]){ System.out.println("Enter the required size of the array :: "); Scanner s = new Scanner(System.in); int size = s.nextInt(); int myArray[] = new int [size]; int sum = 0; System.out.println("Enter the elements of the array one by one "); for(int i=0; i<size; i++){ myArray[i] = s.nextInt(); sum = sum + myArray[i]; } System.out.println("Elements of the array are: "+Arrays.toString(myArray)); System.out.println("Sum of the elements of the array ::"+sum); } }

* The Java code takes the input of the size of the array and then takes the elements of the array. * Then, it stores the sum of the elements of the array in a variable. * Finally, it prints the elements of the array and the sum of the elements of the array.

Questions about programming?Chat with your personal AI assistant