C Program to sum of an Integer array

In this c programming example we will learn how to calculate sum of an integer array 

Algorithm

  1. Take input number to how many items for an integer array
  2. Assign number of integer count to varaible n
  3. Make an Iterate to read array values and assign it ti an array 
  4. Now iterate the array items to read individual integer values and add it to sum variable.
  5. Print the sum of array at the end of the loop

 

Simple C example to calculate sum of an Array

#include <stdio.h>

int main() {
    
    int n;
    
    printf("Enter Array count ");
    
    scanf("%d",&n);
    
    int array[n];
    
    for(int k=0;k<n;k++)
    {
        scanf("%d",&array[k]);
    }
    
   
   int sum, loop;

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

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

   return 0;
}

 

Output

Enter Array count 5
1
1
1
1
1
Sum of array is 5.