Cumulative Sum of an Array in Java

Cumulative Sum of an Array in Java | Program Description:- Write a Java program to find the cumulative sum of an array. Take the array, find the cumulative sum, insert them in the same array, and display the result.

Example1:-
Array = {9, 8, 7, 0, -2, 5}
Cumulative sum = [9, 17, 24, 24, 22, 27]

Example2:-
Array = {-10, 5, 0, -9, 18, 27, -36}
Cumulative sum = [-10, -5, -5, -14, 4, 31, -5]

The cumulative sum is calculated as:- sum = sum + array[i]
Assume array = {9, 8, 7, 0, -2, 5}
sum = 0;
sum = 0 + 9 = 9
sum = 9 +8 = 17
sum = 17 +7 = 24
sum = 24 + 0 = 24
sum = 24 – 2 = 22
sum = 22 + 5 = 27

Procedure to develop the method to find the cumulative sum of an array in Java,
a) Take an array.
b) Declare a variable to store the sum value and initialize it with 0. Assume sum = 0.
c) Traverse through the array.
d) Calculate the sum value as, sum = sum + array[i]
e) Replace the sum value with array[i]
f) Return the array

Prerequisite:- Array in Java, find the length of an array in Java, different ways to print array in Java, find the sum of an array in Java

In this program, we will take the input value for the array from the end-user, but you can take it explicitly. See:- how to take array input in Java. We will use a method to perform the operation. 

There are multiple ways to display an array but here we will use Arrays.toString() method given in java.util.Arrays class which is used to convert an array to String format. The java.util.Arrays class contains many methods related to array operations like sorting an array using sort(), copying an array copyOf() or copyOfRange(), searching an element in an array using binary search, and e.t.c. Learn more about:- Arrays class and methods in Java.

Java Program to Find Cumulative Sum of an Array

import java.util.Arrays;
import java.util.Scanner;

public class ArrayTest {

    public static void main(String[] args) {
        // create Scanner class object to take input
        Scanner scan = new Scanner(System.in);

        // read size of the array
        System.out.print("Enter size of the array: ");
        int n = scan.nextInt();

        // create an int array of size N
        int numbers[] = new int[n];

        // take input for the array
        System.out.println("Enter array elements: ");
        for (int i = 0; i < n; ++i) {
            numbers[i] = scan.nextInt();
        }

        // calculate the cumulative sum of array
        int sum[] = cumulativeSum(numbers);

        // display result
        System.out.println("Cumulative sum = " + Arrays.toString(sum));

        // close Scanner
        scan.close();
    }

    // method to find cumulative sum of array
    public static int[] cumulativeSum(int[] numbers) {
        int sum = 0;

        // traverse through the array
        for (int i = 0; i < numbers.length; i++) {
            sum += numbers[i]; // find sum
            numbers[i] = sum; // replace
        }

        return numbers;
    }
}

Output:-

Enter size of the array: 6
Enter array elements:
9 8 7 0 -2 5
Cumulative sum = [9, 17, 24, 24, 22, 27]

Enter size of the array: 7
Enter array elements:
-10 5 0 -9 18 27 -36
Cumulative sum = [-10, -5, -5, -14, 4, 31, -5]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Cumulative Sum of an Array in Java”

Leave a Comment

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