Sunday 28 December 2014

Cpp Program to calculate Sum of elements in an array


This is a Cpp 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 <iostream>

using namespace std;

int main()
{
    int n, array[100],i,sum=0;
    cout << "Enter how many numbers ? :\t" ;
    cin >> n;
    cout << endl;
    for(i=0;i<n;i++)
    {
        cout << "Enter number " << i+1 << " : \t";
        cin >> array[i];
    }
    for(i=0;i<n;i++)
    {
        sum = sum + array[i];
    }

    cout << " \nSum of " << n << " numbers is : "<< sum;
    return 0;
}
OUTPUT :

Cpp - Sum of elements in an array