1int[] a = {10,20,30,40,50};
2int sum = IntStream.of(a).sum();
3System.out.println("The sum is " + sum);
4
1public class Exercise2 {
2public static void main(String[] args) {
3int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
4int sum = 0;
5
6for (int i : my_array)
7 sum += i;
8System.out.println("The sum is " + sum);
9}
10}
1int sum = Arrays.stream("1 2 3 4".split("\\s+")).mapToInt(Integer::parseInt).sum();
2
1/**
2 * C program to find sum of all elements of array
3 */
4
5#include <stdio.h>
6#define MAX_SIZE 100
7
8int main()
9{
10 int arr[MAX_SIZE];
11 int i, n, sum=0;
12
13 /* Input size of the array */
14 printf("Enter size of the array: ");
15 scanf("%d", &n);
16
17 /* Input elements in array */
18 printf("Enter %d elements in the array: ", n);
19 for(i=0; i<n; i++)
20 {
21 scanf("%d", &arr[i]);
22
23 // Add each array element to sum
24 sum += arr[i];
25 }
26
27 printf("Sum of all elements of array = %d", sum);
28
29 return 0;
30}
1/**
2 * C program to find sum of all elements of array
3 */
4
5#include <stdio.h>
6#define MAX_SIZE 100
7
8int main()
9{
10 int arr[MAX_SIZE];
11 int i, n, sum=0;
12
13 /* Input size of the array */
14 printf("Enter size of the array: ");
15 scanf("%d", &n);
16
17 /* Input elements in array */
18 printf("Enter %d elements in the array: ", n);
19 for(i=0; i<n; i++)
20 {
21 scanf("%d", &arr[i]);
22 }
23
24 /*
25 * Add each array element to sum
26 */
27 for(i=0; i<n; i++)
28 {
29 sum = sum + arr[i];
30 }
31
32
33 printf("Sum of all elements of array = %d", sum);
34
35 return 0;
36}