Java Program to Compute the Sum of Numbers in a List Using For-Loop Last Updated : 08 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List = [1, 2, 3] Output: Sum = 6 Input : List = [5, 1, 2, 3] Output: Sum = 11 Approach 1: Create the sum variable of an integer data type.Initialize sum with 0.Start iterating the List using for-loop.During iteration add each element with the sum variable.After execution of the loop, print the sum. Below is the implementation of the above approach: Java // Java Program to Compute the Sum of // Numbers in a List Using For-Loop import java.util.*; import java.io.*; class GFG { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(5); list.add(6); list.add(7); list.add(10); list.add(9); int sum = 0; for (int i = 0; i < list.size(); i++) sum += list.get(i); System.out.println("sum-> " + sum); } } Outputsum-> 37 Time Complexity : O(n). Auxiliary Space: O(1) Approach 2: Create the sum variable of an integer data type.Initialize sum with 0.Start iterating the List using enhanced for-loop.During iteration add each element with the sum variable.After execution of the loop, print the sum. Below is the implementation of the above approach: Java // Java Program to Compute the Sum of // Numbers in a List Using Enhanced For-Loop import java.util.*; import java.io.*; class GFG { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(5); list.add(6); list.add(7); list.add(10); list.add(9); int sum = 0; for (Integer i : list) sum += i; System.out.println("sum-> " + sum); } } Outputsum-> 37 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Find Sum of N Numbers Using Recursion K kaaruni1124 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 java-list +1 More Practice Tags : Java Similar Reads Java Program to Compute the Sum of Numbers in a List Using While-Loop The task is to compute the sum of numbers in a list using while loop. The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion 2 min read Java Program to Compute the Sum of Numbers in a List Using Recursion ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. I 5 min read Java Program to Find Sum of Natural Numbers Using While Loop While loop arises into play where beforehand there is no conclusive evidence that how many times a loop is to be executed. This is the primary reason as there is no strict tight bound over how many numbers the sum is to be evaluated. Situations in which the output can be displayed using test conditi 3 min read Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve 3 min read Java Program to Find Sum of N Numbers Using Recursion Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Assume/Identify the smaller problem from the problem which is similar to the bigger/ori 4 min read Java Program for Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5}, sum = 6 Output : 3 Pairs with sum 6 are 4 min read Like