Skip to content

Commit 82ad8ce

Browse files
committed
feat: Calculate sum of all elements in an array using enhanced for loop
🧠 Logic: - Declare and initialize an integer array `A` with 10 elements. - Initialize a variable `sum` to 0. - Use an enhanced for loop to iterate over each element in the array and add it to `sum`. - Print the total sum after the loop completes. - (Optional) Includes a commented version using traditional for loop for alternative approach. Signed-off-by: Somesh diwan <[email protected]>
1 parent 5faa8d1 commit 82ad8ce

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Section9ArrayAB/Array IMP/SumOfElementsInArray.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
//Find the sum of all elements in the array.
22

3-
public class SumOfElementsInArray
4-
{
5-
public static void main(String[] args)
6-
{
3+
public class SumOfElementsInArray {
4+
public static void main(String[] args) {
75
int A[]={3,9,7,8,12,6,15,5,4,10};
86

97
int sum = 0;
108

11-
for(int x:A)
12-
{
9+
for(int x:A) {
1310
sum=sum + x;
1411
}
1512
System.out.println("Sum of The Total Number is: "+ sum);
1613

1714
/*
18-
for (int i=0; i<A.length; i++)
19-
{
15+
for (int i=0; i<A.length; i++){
2016
sum=sum+A[i];
2117
}
2218
System.out.println("Sum of total number is: "+sum);

0 commit comments

Comments
 (0)