
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to Find a Cumulative Sum Array in Java?
In Java, Array is an object. It is a non-primitive data type which stores values of similar data types.
As per the problem statement we have to find a cumulative sum array which means array elements will be updated with the sum of the current element and all previous elements.
To find the cumulative sum, the array must contain all numeric values.
In this article, you will see how to find the cumulative sum of an array by using Java programming language. Let’s explore.
To Show You Some Instances
Instance-1
Suppose the original array is {2, 9, 3, 5, 1, 6}
After updating the array with cumulative sum −
Cumulative sum is [2, 11, 14, 19, 20, 26]
Instance-2
Suppose the original array is {2, 9, 11, 5, 15, 6, 10}
After updating the array with cumulative sum −
Cumulative sum is [2, 11, 22, 27, 42, 48, 58]
Instance-3
Suppose the original array is {44, 5, 9, 15, 31, 22, 19, 48}
After updating the array with cumulative sum −
Cumulative sum is [44, 49, 58, 73, 104, 126, 145, 193]
Algorithm
Step 1 − Declare and initialize an integer array. Also declare and initialize an int variable say ‘sum’ as 0.
Step 2 − Traverse through the array.
Step 3 − Calculate the sum value as, sum = sum + array[i]
Step 4 − Replace the sum value with array[i]
Step 5 − Print the elements of the array.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array and for Loop
By Using Static Initialization of Array and while Loop
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array and for Loop
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm, find the cumulative sum of an array. Here, we have used a loop for traversing array.
import java.util.*; public class Main{ //main method public static void main(String[] args) { int sum = 0; //Declaring the array int numbers[] = { 44, 5 , 9, 15, 31, 22, 19, 48 }; // traverse through the array for (int i = 0; i < numbers.length; i++) { // find sum sum += numbers[i]; // replace numbers[i] = sum; } //printing the result System.out.println("Cumulative sum is "); System.out.println(Arrays.toString(numbers)); } }
Output
Cumulative sum is [44, 49, 58, 73, 104, 126, 145, 193]
Approach-2: By Using Static Initialization of Array and while Loop
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm, find the cumulative sum of an array. Here, we have used a while loop for traversing array.
import java.util.*; public class Main{ //main method public static void main(String[] args){ int sum = 0; //Declaring the array int numbers[] = { 44, 5 , 9, 15, 31, 22, 19, 48 }; // traverse through the array int i =0; while(i<numbers.length){ // find sum sum += numbers[i]; // replace numbers[i] = sum; i++; } //printing the result System.out.println("Cumulative sum is "); System.out.println(Arrays.toString(numbers)); } }
Output
Cumulative sum is [44, 49, 58, 73, 104, 126, 145, 193]
Approach-3: By Using User Defined Method
Example
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm to find the cumulative sum of an array.
import java.util.*; public class Main{ //main method public static void main(String[] args){ //calling the user defined method cumulativeSum(); } //user defined method to find cumulative sum array public static void cumulativeSum(){ int sum = 0; //Declaring the array int numbers[] = { 2, 9, 11, 5, 15, 6, 10 }; // traverse through the array for (int i = 0; i < numbers.length; i++) { // find sum sum += numbers[i]; // replace numbers[i] = sum; } //printing the result System.out.println("Cumulative sum is "); System.out.println(Arrays.toString(numbers)); } }
Output
Cumulative sum is [2, 11, 22, 27, 42, 48, 58]
In this article, we explored how to find cumulative sum array by using Java programming language.
- Related Articles
- Converting array of Numbers to cumulative sum array in JavaScript
- How to create a Cumulative Sum Column in MySQL?
- Python program to find Cumulative sum of a list
- How to create a cumulative sum plot in base R?
- How to find the cumulative sum but restarts it if a value is 1 in R?
- How to find the cumulative sum for factor levels in an R data frame?
- How to find the cumulative sum for each row in an R data frame?
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- How to create a column in an R data frame with cumulative sum?
- How to calculate the cumulative sum / running total of a column in Excel?
- Write a program to form a cumulative sum list in Python
- How to find the sum of all the numbers in an array in java?
- Cumulative Sum of a Column in Python Pandas
- Cumulative sum of elements in JavaScript
- How do you find the sum of all the numbers in a java array
