Algorithm
- Initialize an empty array
- Take number of items in array, n
- Loop upto n and take value
- Assign value to the index of that array
- Initialize a sum = 0
- loop upto n and make sum = sum + array [index]
- Print Sum
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main()
{
int array[1000], i, n, sum = 0;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for(i=0; i < n; i++)
{
sum += array[i];
}
printf("Sum = %d", sum);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Enter size of the array : 5
Enter elements in array : 1 2 3 4 5
Enter elements in array : 1 2 3 4 5
Output
Sum = 15
#2 C Program to find sum of array elements using Function
Code -
C Programming
#include <stdio.h>
int sumOfArray(int array[], int n) {
int i = 0, sum = 0;
for(i=0; i < n; i++)
{
sum += array[i];
}
return sum;
}
int main()
{
int array[1000], i, n;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Sum = %d", sumOfArray(array, n));
return 0;
}
Copy The Code &
Try With Live Editor
Input
Enter size of the array : 6
Enter elements in array : 1 2 3 4 5 6
Enter elements in array : 1 2 3 4 5 6
Output
Sum = 21
Demonstration
C Programming Example to find sum of array elements, c program to find sum of array, c program array sum, array sum in c programming