Calculating Sum of All Elements in an Array

Calculating Sum of All Elements in an Array

We can use this code to Calculate Sum of All Elements in array
We can use FOREACH Loop for this task.

int[ ] arr = {13, 36, 63, 556, 990};
int sum = 0;

foreach (int a in arr) {
sum += a;
}

Console.WriteLine(sum);


//This Code Outputs :  “1658” Because 13 + 36 + 63 + 556 + 990 = 1658.
// Our Array ‘arr’ has 5 elements. Sum is initialized with value ‘0’.

// We are adding values of integer ‘a’ from the array to the variable sum for all values of ‘a’

Social Share

Leave a Reply

Your email address will not be published. Required fields are marked *