Java Program to find sum of array elements
Program
import java.util.Scanner;
public class SumOfArrayElements {
public static void main(String[] args) {
System.out.println("Enter the required size of the array: ");
Scanner reader = new Scanner(System.in);
int size = reader.nextInt();
Integer inputArray[] = new Integer[size];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
inputArray[i] = reader.nextInt();
}
double sum = 0;
double average = 0;
for (int i = 0; i < size; i++) {
sum = sum + inputArray[i];
}
System.out.println("Sum of given numbers is: " + sum);
}
}
Output
javac .\SumOfArrayElements.java
java SumOfArrayElements
Enter the required size of the array:
5
Enter the elements of the array:
10
20
30
40
50
Sum of given numbers is: 150.0