/* C Program to find sum of elements in a given array */#include <stdio.h>#define MAX_SIZE 1000// C program to return sum of elements in an array of size nint sum(int arr[], int n){ int sum = 0; // initialize sum int i; // Iterate through all elements and add them to sum for (i = 0; i < n; i++) sum += arr[i]; return sum;}int main(){ int arr[MAX_SIZE]; int n, i; printf("Eneter the number of elements in array\n"); scanf("%d", &n); printf("Enter Elements \n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Sum of the elements is %d", sum(arr, n)); return 0;} |
Output:
Eneter the number of elements in array 3 Enter Elements 12 3 4
No comments:
Post a Comment