Sum of array elements 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 output for details. Sample input 1: 5 2 3 6 8 1 sample output 1: the sum of the elements in the array is 20
Answers
Answered by
1
Answer:
#include <stdio.h>
int main() {
int sum;
int size;
printf("Enter the size of an array ");
scanf("%d",&size);
if(size<=15)
{
int arr[size]; // array of size=5;
arr[1]==size;
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
}
for(int j = 0; j < size; j++){
sum = sum + arr[j]; // same as sum += arr[i];
//print the result
printf("Sum of the array = %d\n",sum);
}
}
else
{
printf("SORRY!! The maximum size of array can only be 15");
}
return 0;
}
Explanation:
i think this is the answer but if you have doubt kindly let me know . so that i can correct my mistakes. cheers!
Similar questions