-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_array.c
More file actions
31 lines (23 loc) · 766 Bytes
/
sum_array.c
File metadata and controls
31 lines (23 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
//function to caluculate the sum of the array
void sum_array(int arr[],int size,int *sum){
int i;
*sum = 0; //initializing the sum with 0
for(i = 0; i < size; i++){
*sum += arr[i]; //sum of each element in the array
}
}
int main(){
int i, size, total;
printf("Enter the size of the array:");
scanf("%d", &size); //input of the size of array
int arr[size];
// input of the elements in the array
printf("Enter the elements of the array:");
for(i = 0; i < size; i++){
scanf("%d", &arr[i]);
}
sum_array(arr, size, &total);//calling the function
printf("The sum of the elements in the array is %d", total);//printing the sum of the elements in the array
return 0;
}