Friday, 10 November 2017

C Program to Calculate the Sum of the Array Elements using Pointer


Code:

#include
#include

void main()
{
    int i, n, sum = 0;
    int *a;

    printf("Enter the size of array A \n");
    scanf("%d", &n);
    a = (int *) malloc(n * sizeof(int));
    printf("Enter Elements of First List \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", a + i);
    }
    for (i = 0; i < n; i++)
    {
        sum = sum + *(a + i);
    }
    printf("Sum of all elements in array = %d\n", sum);
}

Output:

Enter the size of array A
5
Enter Elements of First List
4
9
10
56
100
Sum of all elements in array = 179

More Imp Topics:




100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...