HomePythonPython Program to Find the Cumulative Sum of a List

Python Program to Find the Cumulative Sum of a List

Python’s versatility shines when it comes to solving various programming challenges. In this example, we’ll create a Python program to find the cumulative sum of a given list of numbers.

Problem Statement

Write a Python program to calculate and display the cumulative sum of a given list. Given an input list, your program should generate an output list where each element represents the cumulative sum of all preceding elements, including itself.

Python Program to Find the Cumulative Sum of a List

def cumulative_sum(input_list):
    cumulative = []
    current_sum = 0
    
    for num in input_list:
        current_sum += num
        cumulative.append(current_sum)
    
    return cumulative

# Input and Output
input_list = [3, 5, 2, 8, 10]
result = cumulative_sum(input_list)
print("Input List:", input_list)
print("Cumulative Sum List:", result)

Input / Output

Python Program to Find the Cumulative Sum of a List

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...