Skip to content

Commit b40a34b

Browse files
committed
feat: Calculate sum of all elements in the array including negatives
🧠 Logic: - Initialize `sum` to 0. - Use enhanced for loop (`for-each`) to iterate over all elements in the array. - Accumulate each value in `sum`, regardless of whether it’s positive or negative. - Print the final computed sum. Signed-off-by: Somesh diwan <[email protected]>
1 parent a574882 commit b40a34b

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

Section9ArrayAB/Array 2.0/src/SumOfArray.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ public static void main(String[] args) {
33
int[] arr={-2, 4, 55,-4,11,-55,11, -55,-11,2};
44
int sum=0;
55

6-
for(int i: arr)
7-
{
6+
for(int i: arr) {
87
sum+=i;
98
}
109
System.out.println(sum);
1110

1211
/*
13-
for (int i=0; i<arr.length;i++)
14-
{
12+
for (int i=0; i<arr.length;i++) {
1513
sum+=arr[i];
1614
}
1715
System.out.println(sum);

0 commit comments

Comments
 (0)