Advertisement

C++ Program to find the sum of array elements using the function


In this program, You will learn how to find the sum of array elements using the function in C++.


Sum of array elements using the function

sumOfArray(arr, size);

Example: How to find the sum of array elements using the function in C++

#include<iostream>
using namespace std;

void sumOfArray(int arr[], int n) {
    int s = 0;
    for (int i = 0; i < n; i++) {
        s = s + arr[i];
    }
    cout << "Sum of array elements:" << s;
}

int main() {

    int arr[10], i, n;

    cout << "Enter size of an array:";
    cin>>n;

    cout << "Enter array elements:";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    sumOfArray(arr, n);

    return 0;
}

Output:

Enter size of an array:5
Enter array elements:10 20 30 40 50
Sum of array elements:150