Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

sum of 1D array(JAVA)

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

public class LeetCode_RunningSum {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int[] arr = {1,2,3,4};

        System.out.println(Arrays.toString(out(arr)));

    }

    static int[] out(int[] nums){

        int sum = 0;
        int[] arr = new int[nums.length];

        for (int i = 0; i < nums.length; i++){
            sum += nums[i];
            arr[i] = sum;
        }

        return arr;
    }
}
Comment

Running Sum of 1d Array Java

class Solution {
    public int[] runningSum(int[] nums) {
    int[] ans = new int[nums.length];
    ans[0] = nums[0];
    for (int i = 1; i < nums.length; i++)
        ans[i] = ans[i-1] + nums[i];
    return ans;
    }
}
Comment

Sum of Array Elements JAVA

// Calculate the sum of all elements of an array

class Main {
 public static void main(String[] args) {
  
   // an array of numbers
   int[] numbers = {3, 4, 5, -5, 0, 12};
   int sum = 0;

   // iterating through each element of the array 
   for (int number: numbers) {
     sum += number;
   }
  
   System.out.println("Sum = " + sum);
 }
}
Comment

sum of array in java

Input : arr[] = {1, 2, 3}
Output : 6
1 + 2 + 3 = 6

Input : arr[] = {15, 12, 13, 10}
Output : 50
15 + 12 + 13 + 10 = 50
Comment

PREVIOUS NEXT
Code Example
Java :: conditions in for loop java 
Java :: how to do stuff with a scoreboard minecraft 
Java :: java convert integer to string 
Java :: sort list of string lexicographically java 
Java :: java long to int 
Java :: how to find the largest number in a 2d array java 
Java :: all devisor of a number java 
Java :: read integer input java 
Java :: How do I make a splash screen? 
Java :: java print hex format 
Java :: how to add elements to an array in java dynamically 
Java :: hashmap foreach 
Java :: android translate string java 
Java :: java selenium send keys numbers 
Java :: android check if app is running 
Java :: android sharedpreferences 
Java :: select max value from list java 
Java :: clear jtable rows java 
Java :: Java Scanner nextLine() 
Java :: AmazonS3ClientBuilder 
Java :: empty array java 
Java :: calculator with java 
Java :: how to use random bound on doubles java 
Java :: from string to date 
Java :: replace substring at index java 
Java :: play mp3 android java 
Java :: from string to double java 
Java :: split method in java 
Java :: filter arraylist java 
Java :: print java 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =