Java Program to Compute the Sum of Numbers in a List Using While-Loop Last Updated : 07 Oct, 2022 Comments Improve Suggest changes Like Article Like Report 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 of elements. Approach Create a list.Create two int variable one for storing the sum of the array and the second is to help us to execute the while loop.Add some elements in the list.Execute the while loop until the count variable in less than the size of the list.Get elements of list one by one using get() method and add the element with sum variable.Increment the count variable. Code Java // Java Program to Compute the Sum of Numbers in a List // Using While-Loop import java.util.*; public class GFG { public static void main(String args[]) { // create a list List<Integer> list = new ArrayList<Integer>(); int sum = 0; int count = 0; // add some elements in list list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); // execute while loop until the count less than the // list size. while (list.size() > count) { // add list values with sum variable one-by-one. sum += list.get(count); // increment in count variable count++; } // print sum variable System.out.println(" The sum of list is: " + sum); } } Output The sum of list is: 15 Time complexity: O(n) where n is size of list Auxiliary space:O(n) since using a List to store numbers Comment More infoAdvertise with us Next Article Java Program to Compute the Sum of Numbers in a List Using While-Loop mukulsomukesh 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 For-Loop 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 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 Reverse a Number and find the Sum of its Digits Using do-while Loop Problem Statement: The number is supposed to be entered by the user be it any random number lying within the primitive data-type holding the number. First, the number needs to be reversed. Secondary the sum of the number is to be calculated with the constraint to use a do-while loop. do-while loop: 6 min read Perfect Number Program in Java Using While Loop The number which is equal to the sum of its divisors is called a perfect number. Read the entered long number, assigned to the long variable n. While loop iterates until the condition (i<=n/2) is false. If the remainder of n/i=0 then add i value to the sum and increase the i value. After all the 2 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 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 The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input: 10->6->8->4->12, n = 2 Output: 16 Sum of last two nodes: 12 + 4 = 16 Input: 15->7->9->5->16->14, n = 4 10 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 Like