Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Program to Find the Sum of Elements in an Array

#include <stdio.h>

// Function to calculate the sum of elements in an array
int calculateArraySum(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

int main() {
int size;

// Taking input for the size of the array
printf(“Enter the size of the array: “);
scanf(“%d”, &size);

int array[size];

// Taking input for array elements
printf(“Enter %d elements:n”, size);
for (int i = 0; i < size; i++) {
scanf(“%d”, &array[i]);
}

// Calling the function to calculate the sum and printing the result
int sum = calculateArraySum(array, size);
printf(“Sum of elements in the array: %dn”, sum);

return 0;
}