Find the sum of all array elements with C.
#include <stdio.h>
int main()
{
int arr[101];
int i, n, sum = 0;
/* Input size of the array */
printf("Enter size of the array: ");
scanf("%d", &n);
printf("Enter %d elements in the array: ", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
sum = sum + arr[i];
}
printf("Sum of all elements of array = %d", sum);
return 0;
}
Output:-
Enter size of the array : 10
Enter 10 elements in the array : 1 2 3 4 5 6 7 8 9 10
Sum of all elements of array = 55
0 Comments