C++ Program to find 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 function

sumOfArray(arr,size);


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

Run it Now
#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 << "\nSum of Array elements is :" << s;
}

int main() {

    int arr[10], i, n;

    cout << "Enter Size of an Array :";
    cin>>n;

    cout << "\nEnter 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 :3 4 5 6 77
Sum of Array elements is :95