C Program to find Sum of Elements in an Array

Sum of Elements in an Array

Today we will learn how to find sum of elements in an array is easy task when you know how to iterate through array elements. In this problem we will explain you C approach to find sum of array elements inputted by the user.
Here is the solution of this query in C Language.

checking if two strings match where one string contains wildcard characters

Wild Card characters are characters like ‘ * ‘ and ‘ ? ‘. In this java program we will be checking that if two strings match where one string contains wildcard characters. We will be using the concept of ternary operator and pointers in this program, to increase the efficiency and reduce the complexity of the program. For better understanding of this program, you should atleast be having a good knowledge of ternary operator and pointers

Working:-

1. Input the size of array and store the value in m and arr[m].
2. To store sum of array elements, initialize a variable sum = 0.
3. To find sum of all elements, iterate through each element and add the current element to the sum.
4. Inside the loop add the current array element to sum i.e.sum = sum + arr[i] or sum += arr[i].

C Program:-

```#include
#define ARR_SIZE 100
int main()
{
int arr[ARR_SIZE];
int i,m,size,sum=0;
printf("Enter size of the array: ");// Input size of the array
scanf("%d", &m);

printf("Enter %d elements in the array: ",m);// Input elements in array
for(i=0; i<m; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<m; i++)  //Add each elements to sum array
{
sum = sum + arr[i];
}
printf("Sum of elements in an array = %d", sum);
return 0;
}
```
```Output: Enter size of the array: 5
Enter 5 elements in the array: 5 5 5 5 5
Sum of elements in an array = 25```

One comment on “C Program to find Sum of Elements in an Array”

• Ankit

If we have to input array from user and then we have to get the sum of the elements. Here is the code…
from array import*
arr=array(‘i’,[])
sum=0
n=int(input(“enter the length of array”))
for i in range(n):
x=int(input(“enter the elements”))
arr.append(x)
sum=sum+x
print(arr)
print(sum)
Input-2,3,4,5,6 if n=5
Output- 20