Java Program to find the sum of array elements
In this program, You will learn how to find the sum of array elements in java.
List is: 10 20 30 40 50
150 = 10 + 20 + 30 + 40 + 50
Example: How to find the sum of array elements in java.
import java.util.Scanner;
class Main {
public static void main(String args[]) {
int i, s = 0;
int arr[] = new int[5];
Scanner sc = new Scanner(System.in);
System.out.print("Enter 5 numbers:");
for (i = 0; i < 5; i++) {
arr[i] = sc.nextInt();
}
/* Sum of Array */
for (i = 0; i < 5; i++) {
s = s + arr[i];
}
System.out.println("Sum of all elements:" + s);
}
}
Output:
Enter 5 numbers:10 20 30 40 50
Sum of all elements:150