Sum of array in java – Java Program to Find Cumulative Sum of an Array

Sum of array in java: In the previous article, we have seen Java Program to Convert an Array-List to Array

In this article we are going to see how we can find the cumulative sum of an array in Java.

Java Program to Find Cumulative Sum of an Array

Sum of an array java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the cumulative sum of an array.

Method-1: Java Program to Find Cumulative Sum of an Array By Static Initialization of Array Elements

Approach:

  • Take an array with elements in it.
  • Print the array elements.
  • Find the sum of all elements by iterating using a for loop and then initialize the cumulative sum to the elements itself.
  • Print the cumulative sum array.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12,2,34,20,54,6};

        System.out.print("The array elements are ");
        // Prints the array elements
        printArray(arr);
        // Adds the elements to form the cumulative array
        int sum = 0;        
        for(int i=0;i<arr.length;i++)
        {
            sum+=arr[i];
            arr[i] = sum;
        }
        // Prints the cumulative sum array
        System.out.print("The cumulative sum array is ");
        printArray(arr);
    }
    // Function to print the array
    static void printArray(int arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

The array elements are 12 2 34 20 54 6 
The cumulative sum array is 12 14 48 68 122 128

Method-2: Java Program to Find Cumulative Sum of an Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Print the array elements.
  • Find the sum of all elements by iterating using a for loop and then initialize the cumulative sum to the elements itself.
  • Print the cumulative sum array.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int arr[] = new int[num]; 
        System.out.print("Enter the elements: "); 
        for (int i = 0; i < num; i++) 
        { 
            arr[i] = sc.nextInt(); 
        }

        System.out.print("The array elements are ");
        // Prints the array elements
        printArray(arr);
        // Adds the elements to form the cumulative array
        int sum = 0;        
        for(int i=0;i<arr.length;i++)
        {
            sum+=arr[i];
            arr[i] = sum;
        }
        // Prints the cumulative sum array
        System.out.print("The cumulative sum array is ");
        printArray(arr);
    }
    // Function to print the array
    static void printArray(int arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

Enter the number of elements in the array: 10
Enter the elements: 5 10 15 20 25 30 35 40 45 50
The array elements are 5 10 15 20 25 30 35 40 45 50 
The cumulative sum array is 5 15 30 50 75 105 140 180 225 275

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: