# Java Program to Compute the Running Total of a List

## Introduction

The Java program to compute the running total of a list is a simple program that demonstrates the use of loops and lists in Java. The program takes a list of integers as input and computes the running total of the numbers in the list, which is the cumulative sum of all the numbers up to a particular point. The program initializes a variable called runningTotal to 0 and then loops through the list of numbers using a for loop. For each number in the list, the program adds it to the running total variable and prints out the current value of the running total. After the loop has finished, the running total variable holds the total sum of all the numbers in the list.

The program has a time complexity of O(n), where n is the number of elements in the list, because it loops through the list once and performs a constant-time operation for each element in the list. This program can be useful in a variety of contexts, such as in financial or mathematical applications where the running total of a set of values needs to be calculated.

## Example 1

### Approach

• Import the necessary packages (in this case, we need java.util.ArrayList and java.util.List).

• Create an instance of the ArrayList class to hold the list of numbers we want to compute the running total for.

• Initialize a variable to hold the running total and set it to 0.

• Loop through the list using a for loop.

• For each number in the list, add it to the running total and print out the current value of the running total using the println() method.

• After the loop has finished, the running total variable will hold the total sum of all the numbers in the list.

Here's an example Java program to compute the running total of a list −

import java.util.ArrayList;
import java.util.List;

public class RunningTotal {

public static void main(String[] args) {

List<Integer> numbers = new ArrayList<>(); // create a list of integers

// add some numbers to the list

int runningTotal = 0; // initialize running total variable to 0

// loop through the list and compute the running total
for (int number : numbers) {
runningTotal += number;
System.out.println("Running Total: " + runningTotal);
}
}
}


### Explanation

This program creates a list of integers and adds some numbers to it. It then initializes a variable called runningTotal to 0 and loops through the list. For each number in the list, it adds the number to the runningTotal variable and prints out the current running total. At the end of the loop, the program will have computed the running total of all the numbers in the list.

### Output

Running Total: 5
Running Total: 15
Running Total: 30
Running Total: 50


## Example 2

This program uses a while loop to iterate through the list and compute the running total, which is slightly different from the for loop used in the first example program. However, the basic approach is the same: we initialize a running total variable to 0, loop through the list, add each element to the running total, and print out the current value of the running total at each step. Overall, this program is a simple and effective way to compute the running total of a list of numbers in Java.

### Approach

• Import the necessary packages (in this case, we need java.util.ArrayList and java.util.List).

• Create an instance of the ArrayList class to hold the list of numbers we want to compute the running total for.

• Initialize a variable to hold the running total and set it to 0.

• Initialize an index variable i to 0.

• Loop through the list using a while loop with the condition i < numbers.size().

• For each number in the list, add it to the running total and print out the current value of the running total using the println() method.

• After the loop has finished, the running total variable will hold the total sum of all the numbers in the list.

Here's another example Java program to compute the running total of a list. This program is slightly different from the previous example in that it uses a while loop instead of a for loop to compute the running total −

import java.util.ArrayList;
import java.util.List;

public class RunningTotal {

public static void main(String[] args) {

List<Integer> numbers = new ArrayList<>(); // create a list of integers

// add some numbers to the list

int runningTotal = 0; // initialize running total variable to 0
int i = 0; // initialize index variable to 0

// loop through the list using a while loop
while (i < numbers.size()) {
runningTotal += numbers.get(i); // add the current number to the running total
System.out.println("Running Total: " + runningTotal);
i++; // increment the index variable
}
}
}


### Explanation

This program is similar to the previous one in that it creates a list of integers, adds some numbers to it, initializes a variable called runningTotal to 0, and computes the running total of the numbers in the list. However, instead of using a for loop to iterate through the list, it uses a while loop and an index variable i to access each element of the list.

The while loop condition checks if the index variable i is less than the size of the list, which means that the loop will continue until we have reached the end of the list. Within the loop, we add the current number to the running total variable, print out the current value of the running total, and increment the index variable.

### Output

Running Total: 2
Running Total: 6
Running Total: 12
Running Total: 20
Running Total: 30


## Conclusion

• Computing the running total of a list of numbers is a common task in programming, and there are multiple ways to implement it in Java. Two example programs were presented above, one using a for loop and one using a while loop, but both programs follow a similar approach of initializing a running total variable to 0, iterating through the list of numbers, and adding each element to the running total.

• The time complexity of both programs is linear in the size of the input list, or O(n), because they loop through the list once and perform a constant-time operation for each element in the list. The space complexity of the programs is also constant, or O(1), because they use a constant amount of memory to store the input list, the running total variable, and any other variables used in the program.

Updated on: 10-Apr-2023

249 Views 