- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Java Program to Compute the Sum of Numbers in a List Using While-Loop

## Introduction

The Java program to compute the sum of numbers in a list using a while-loop is a simple program that takes a list of integers and computes their sum using a while-loop construct. In this program, an ArrayList of integers is created, and a few numbers are added to the list. The program then uses a while-loop to iterate over each element in the list, adding each element to a variable "sum", which keeps track of the running sum of the numbers. Once the loop has finished, the final value of "sum" is printed to the console, which is the sum of all the numbers in the list.

This program demonstrates a common technique for processing collections of data in programming, namely, using a loop to iterate over each element in the collection and performing some computation or transformation on each element. The program also highlights the use of ArrayLists in Java, which are a commonly used data structure for storing collections of data.

## Example 1

### Approach

First, we create an ArrayList of integers called "numbers" and add some numbers to it. In this program, we have added the numbers 1, 2, 3, 4, and 5 to the list.

We then declare two variables − "sum" and "i". "sum" is initialized to 0, as we want to start computing the sum of the numbers from zero. "i" is initialized to 0, as this is the index of the first number in the list that we want to start adding to the sum.

We start a while-loop that will continue as long as "i" is less than the size of the list. The size of the list can be obtained using the "size" method of the ArrayList class.

Inside the loop, we use the "get" method of the ArrayList class to retrieve the current number in the list, and add it to the sum. We then increment "i" by 1 to move to the next number in the list.

Once the loop is finished, we have computed the sum of all the numbers in the list. We print out the sum using the "println" method.

Overall, this approach is relatively simple and straightforward. It uses a while-loop to iterate over the elements of the list and accumulate their sum in a separate variable. This is a common technique for processing lists and other collections of data in programming.

Here's a Java program that computes the sum of numbers in a list using a while-loop −

import java.util.ArrayList; public class SumOfListUsingWhileLoop { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); int sum = 0; int i = 0; while (i < numbers.size()) { sum += numbers.get(i); i++; } System.out.println("The sum of the numbers in the list is: " + sum); } }

### Explanation

In this program, we first create an ArrayList of integers called "numbers" and add some numbers to it. Then, we declare two variables: "sum" (initialized to 0) to store the sum of the numbers in the list, and "i" (initialized to 0) to keep track of the index of the current number we are adding to the sum.

Next, we start a while-loop that continues as long as "i" is less than the size of the list. Inside the loop, we add the current number in the list (retrieved using the "get" method) to the sum, and increment "i" to move to the next number in the list.

Finally, once the loop is finished, we print out the sum of the numbers in the list using the "println" method.

### Output

The sum of the numbers in the list is: 15

## Example 2

### Approach

First, we create an ArrayList of Double values called "numbers" and add some numbers to it. In this program, we have added the numbers 2.5, 3.7, 1.8, 4.2, and 2.9 to the list.

We then declare two variables: "sum" and "i". "sum" is initialized to 0.0, as we want to start computing the sum of the numbers from zero. "i" is initialized to 0, as this is the index of the first number in the list that we want to start adding to the sum.

We start a while-loop that will continue as long as "i" is less than the size of the list. The size of the list can be obtained using the "size" method of the ArrayList class.

Inside the loop, we use the "get" method of the ArrayList class to retrieve the current number in the list, and add it to the sum. We then increment "i" by 1 to move to the next number in the list.

Once the loop is finished, we have computed the sum of all the numbers in the list. We print out the sum using the "println" method.

Overall, the approach used in this program is very similar to the approach used in the previous example. We are using a while-loop to iterate over the elements of the list and accumulate their sum in a separate variable. However, in this case, we are using Double values instead of Integer values, which allows us to include decimal places in our numbers. Additionally, we are using a different set of numbers in the list to demonstrate that the program can handle a variety of input values.

Here's another example of a Java program that computes the sum of numbers in a list using a while-loop −

import java.util.ArrayList; public class SumOfListUsingWhileLoop2 { public static void main(String[] args) { ArrayList<Double> numbers = new ArrayList<Double>(); numbers.add(2.5); numbers.add(3.7); numbers.add(1.8); numbers.add(4.2); numbers.add(2.9); double sum = 0.0; int i = 0; while (i < numbers.size()) { sum += numbers.get(i); i++; } System.out.println("The sum of the numbers in the list is: " + sum); } }

### Explanation

In this program, we have created an ArrayList of Double values called "numbers" and added some numbers to it. Then, we have declared two variables: "sum" (initialized to 0.0) to store the sum of the numbers in the list, and "i" (initialized to 0) to keep track of the index of the current number we are adding to the sum.

Next, we start a while-loop that continues as long as "i" is less than the size of the list. Inside the loop, we add the current number in the list (retrieved using the "get" method) to the sum, and increment "i" to move to the next number in the list.

Finally, once the loop is finished, we print out the sum of the numbers in the list using the "println" method. Note that in this example, we are using Double values instead of Integer values, which allows us to include decimal places in our numbers.

### Output

The sum of the numbers in the list is: 1.5

## Conclusion

In this article, we have discussed two examples of Java programs that compute the sum of numbers in a list using a while-loop. In both examples, we used a similar approach to iterate over each element in the list, accumulate their sum in a separate variable, and then output the result.

We also discussed the time and space complexity of these programs. The time complexity of both programs is O(n), where n is the number of elements in the list, as we need to iterate over each element in the list to compute their sum. The space complexity of both programs is O(1), as we only need to use a few variables to store the input list, the running sum, and the loop counter, and the size of these variables does not depend on the size of the input.