Easy Tutorial
❮ C Function Perror C Exercise Example50 ❯

C Language Example - Sum of Array Elements

C Language Examples

Using a for loop to iterate through the elements and sum them up:

Example

#include <stdio.h>

int main() {
   int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
   int sum, loop;

   sum = 0;

   for(loop = 9; loop >= 0; loop--) {
      sum = sum + array[loop];      
   }

   printf("Sum of elements: %d", sum);   

   return 0;
}

Output result:

Sum of elements: 45

C Language Examples

❮ C Function Perror C Exercise Example50 ❯