Java Program to Compute the Sum of Numbers in a List Using For-Loop Last Updated : 08 Sep, 2022 Comments Improve Suggest changes 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 Compute the Sum of Numbers in a List Using For-Loop 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 Java Program to Iterate Over Arrays Using for and for-each Loop In Java, arrays are a collection of elements of the same type. To access and manipulate elements, we can use iteration techniques such as the for loop and for-each loop (also known as the enhanced for loop).Program to Iterate Over Arrays Using for and for-each Loop in JavaIn the below example, we wi 1 min read Java Program to Find Average of Two Lists To calculate the average of two lists in Java we first need to combine the two lists into one. we can do this using the addAll() method of the ArrayList class. Once you have combined the lists we can calculate the average by summing up all the elements in the combined list and dividing by the total 2 min read Java Program to Access the Part of List as List A List is an ordered sequence of elements stored together to form a collection. A list can contain duplicate as well as null entries. A list allows us to perform index-based operations, that is additions, deletions, manipulations, and positional access. Java provides an in-built interface <<ja 4 min read Java Program to Find Sum of Array Elements Given an array of integers. Write a Java Program to find the sum of the elements of the array. Examples: Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50 15 + 12 + 13 + 10 = 50 An array is a data structure that contains a group of elements. Typically th 3 min read Like