Compute Sum of Numbers in a List Using While Loop in Java



In this article, we will learn to compute the sum of numbers in a list using Java with a while-loop. We will look at two examples: one that sums a list of integers and another that sums a list of double values. For each example, we will create an ArrayList to store the numbers, and then use a while-loop to iterate through the list. During the iteration, we will add each number to a variable called "sum," which will keep track of the total. This will help you understand how to use loops and ArrayLists in Java effectively.

Different approaches

Following are the different approaches to computing the sum of numbers in a list using while-loop?

Summing a list of integers using a while-loop

This approach is relatively simple. 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.

  • We create an ArrayList of integers called numbers and add 1, 2, 3, 4, and 5 to it.
  • Two variables are declared: sum (initialized to 0) to store the total, and i (initialized to 0) as the index for iteration.
  • A while-loop runs as long as i is less than the size of the list.
  • Inside the loop, the get method retrieves the current number and adds it to the sum. Then, i is incremented.
  • Once the loop ends, the sum of the numbers is printed using println.

Example

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 create an ArrayList of integers called "numbers" and initialize two variables: "sum" to store the total (starting at 0) and "i" to track the index (starting at 0). A while-loop runs while "i" is less than the list size, adding each number (retrieved via the "get" method) to "sum" and incrementing "i". After the loop, the final sum is printed using the "println" method.

Output

The sum of the numbers in the list is: 15

Summing a list of double values using a while-loop

This program uses a similar approach as before, with a while-loop to sum the list elements. The difference is we use Double values to handle decimal numbers and demonstrate the program's ability to handle different inputs.

  • First, we create an ArrayList of Double values called "numbers" and add numbers like 2.5, 3.7, 1.8, 4.2, and 2.9 to the list.
  • We declare two variables: sum and i. Sum is initialized to 0.0, and i is initialized to 0, representing the starting index.
  • We use a while-loop that runs as long as i is less than the size of the list. The list size is obtained using the size() method.
  • Inside the loop, the get method retrieves the current number from the list, and it is added to the sum. Then, i is incremented by 1 to move to the next element.
  • Once the loop ends, the total sum of all numbers is printed.

Example

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 create an ArrayList of Double values called "numbers" and add some numbers to it. We also declare two variables: "sum" (starting at 0.0) to hold the total and "i" (starting at 0) to track the current index. A while-loop runs while "i" is less than the list size, adding each number (retrieved with the "get" method) to "sum" and incrementing "i." After the loop, we print the total using the "println" method. This example uses Double values, allowing us to include decimal places.

Output

The sum of the numbers in the list is: 1.5
Updated on: 2024-10-21T17:42:02+05:30

616 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements