Write a program that takes in 10 integers from the user and stores them inside an array. Then, create a method that returns the sum of all the integers inside a given array, and use that method to calculate the sum of the 10 integers. Print the result out.
top of page
bottom of page
import java.util.*;
public class Sum1 {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
int a = sc.nextInt();
arr[i] = a;
}
System.out.print(sum(arr));
}
public static int sum(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum = array[i] + sum;
}
return sum;
}
}