OneBite.Dev - Coding blog in a bite size

loop array in C

Code snippet on how to loop array in C

  int Array[5] = {2, 3, 4, 5, 6};
  int Sum = 0;
 
  for (int i=0; i<5; i++) 
  {
     Sum = Sum + Array[i];
  }

This code is used to loop through an array to calculate the sum of the numbers in the array. First, an array named “Array” is created with 5 elements (2, 3, 4, 5, 6). A variable “Sum” is initialized to 0. The loop then begins at index 0, and checks to make sure that it is less than 5 (the number of elements in the array). During each iteration of the loop, the number at the current index (i) of the array is added to the “Sum” variable. Once the loop is done processing, the final value of Sum would be the sum of all of the elements in the array.

c