forked from yuvrajjwala/-sumOfArray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_array.c
More file actions
52 lines (42 loc) · 901 Bytes
/
sum_array.c
File metadata and controls
52 lines (42 loc) · 901 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* C program to find sum of all elements of array
**/
#include <stdio.h>
#include <stdlib.h>
/*Sum of the array*/
void sum_array(int arr[], int n)
{
int i,sum = 0;
/**
* Add each array element to sum
**/
for(i = 1; i <= n; i++)
{
sum += arr[i];
}
/**
* Displaying the sum of the
* elements of the array given
* by the user.
**/
printf("\nSum of all elements of array = %d", sum);
}
/*The driver code*/
int main()
{
int *arr,n,i;
/* Input size of the array */
printf("Enter size of the array: ");
scanf("%d", &n);
/*dynamic memory allocation*/
arr=(int *)malloc(n*sizeof(int));
/* Input elements in array */
printf("Enter %d elements in the array: ", n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
/*Function call*/
sum_array(arr,n);
return (0);
}