There are following way to print the sum of array elements in java.
Here first we create array object with any size.
Set sum=0.
Now we create scanner class object to pass input parameter.
From for loop to read input and store a variable arSize[i].
Now sum of input parameter elements from for loop.
sum = sum + arSize[i];
Print “Sum of all the elements of an array:”
import java.util.Scanner;
public class ArraySum {
public static void main(String[] args) {
// declare size of input array
int arSize[] = new int[10];
System.out.println("size of input array ");
Scanner sc = new Scanner(System.in);
int number = Integer.parseInt(sc.next());
int sum = 0;
System.out.println("input array elements ");
for (int i = 0; i < number; i++) {
arSize[i] = sc.nextInt();
}
System.out.println("sum of input array elements");
for (int i = 0; i < number; i++) {
sum = sum + arSize[i];
}
System.out.println(sum);
}
}
Output :- size of input array 2 input array elements 1 3 sum of input array elements 4