java stream sum() code example
Example 1: java running sum
public int[] runningSum(int[] nums) {
int len = nums.length;
for(int i = 1; i < len; i++){
nums[i] += nums[i-1];
}
return nums;
}
Example 2: sum of numbers in java
public class SumNatural {
public static void main(String[] args) {
int num = 100, sum = 0;
for(int i = 1; i <= num; ++i)
{
// sum = sum + i;
sum += i;
}
System.out.println("Sum = " + sum);
}
}