Home

A program to find the sum of array elements

C Programming Language 051

The following code shows how to write a program to find the sum of array elements and print them.

Program

#include <stdio.h>

void main()
{
    int a[30], n, i, sum = 0;
    printf("Enter the size of array: ");
    scanf("%d", &n);

    printf("Enter the values:\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    for (i = 0; i < n; i++)
    {
        sum = sum + a[i];
    }

    printf("Sum of elements: %d", sum);
    printf("\n");
}

Result

Enter the size of array:4
Enter the values:
1
3
2
5
Sum of elements: 11


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments