C program to find the sum of elements of a dynamic array

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *a;
    int i,sum=0,n;
    printf("\nEnter the no. of elements\n");
    scanf("%d",&n);
    a=malloc(n*sizeof(int));//allocating dynamic memory
    printf("\nEnter %d elements\n",n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
        sum=sum+a[i];
    printf("\nSum= %d\n",sum);
    return 0;
}

OUTPUT:

Enter the no. of elements
5

Enter 5 elements
10 20 30 40 50

Sum= 150

Popular Posts