Online Snippets Library

Create C Program to calculate sum of an array

To Calculate sum of array we need to use a loop and sum up all values of the array.

C Program
#include <stdio.h>

int main() {
   int array[5] = {1, 2, 3, 4, 5};
   int sum, loop;

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

   printf("Sum of array is %d.", sum);   

   return 0;
}

Output

Sum of array is 15.