Find the sum of all the elements of the array
C program to find the sum of all the elements of the array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int arr[MAX];
int i, n, sum;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&arr[i]);
}
sum = 0;
for(i=0;i<n;i++)
{
sum = sum + arr[i];
}
printf("\nSum of the elements of the array is\t:%d",sum);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements
Element 1 :1
Element 2 :2
Element 3 :3
Element 4 :4
Element 5 :5
Element 6 :6
Element 7 :7
Element 8 :8
Sum of the elements of the array is :36