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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.