public class Exercise2 {
public static void main(String[] args) {
int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;
for (int i : my_array)
sum += i;
System.out.println("The sum is " + sum);
}
}
// Calculate the sum of all elements of an array
class Main {
public static void main(String[] args) {
// an array of numbers
int[] numbers = {3, 4, 5, -5, 0, 12};
int sum = 0;
// iterating through each element of the array
for (int number: numbers) {
sum += number;
}
System.out.println("Sum = " + sum);
}
}
static int arr[] = { 12, 3, 4, 15 };
// method for sum of elements in an array
static int sum()
{
int sum = 0; // initialize sum
int i;
// Iterate through all elements and add them to sum
for (i = 0; i < arr.length; i++)
sum += arr[i];
return sum;
}