By Team Escalera
on 31 Oct 2017 03:27 PM
  • interview questions
0 comments

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’

Comments (0)

Leave A Comment