Friday, 10 November 2017

C Program to Calculate Sum of all Elements of an Array using Pointers as Arguments


Code:

#include

void main()
{
    static int array[5] = { 200, 400, 600, 800, 1000 };
    int sum;

    int addnum(int *ptr);

    sum = addnum(array);
    printf("Sum of all array elements = %5d\n", sum);
}
int addnum(int *ptr)
{
    int index, total = 0;
    for (index = 0; index < 5; index++)
    {
        total += *(ptr + index);
    }
    return(total);
}

Output:

Sum of all array elements =  3000

More C Programs:



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...