Thursday, 8 January 2015

C Program to calculate Sum Of Elements in an array


This is a C Program which calculates the sum of all elements in an array.

Here initially first element in array and  0 are added the obtained sum is added to 2nd element and so on until the last element in the array.

PROGRAM :
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n,sum=0;
    printf("How many elements do u want to add ?\t");
    scanf("%d",&n);
    int array[n];
    for(i=0;i<n;i++)
    {
        printf("\nEnter number %d :\t",i+1);
        scanf("%d",&array[i]);
    }

    for(i=0;i<n;i++)
    {
        sum = sum+array[i];
    }
    printf("\nThe sum of %d numbers is %d\n",n,sum);
    return 0;
}

OUTPUT :
C - Sum Of Elements in an Array


Related Programs:
Sum Of Elements in an array ( Cpp )
Sum Of Elements in an array ( Java)
Largest Element in an array( C )
Largest Element in an array ( Cpp )
Largest Element in an array( Java )

No comments:

Post a Comment