Write a C program to find the sum of the elements in an array.
Input Format:
Input consists of n+1 integers.
The first integer corresponds to ‘n’, the size of the array.
The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.
Output Format:
Refer sample input and output for formatting details.
Sample Input 1:
5
2
3
6
8
1
Sample Output 1:
The sum of the elements in the array is 20
Program Code:
#include <stdio.h>
#define MAX_SIZE 15
int main()
{
int arr[MAX_SIZE];
int i, n, sum=0;
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<n; i++)
{
sum = sum + arr[i];
}
printf("The sum of the elements in the array is %d", sum);
return 0;
}
Post A Comment:
0 comments: