From a8828bd8ce9833a0ed96c129e8a964cfe6a5b362 Mon Sep 17 00:00:00 2001 From: Hardik9991 <73008712+Hardik9991@users.noreply.github.com> Date: Sat, 17 Oct 2020 10:48:36 +0530 Subject: [PATCH] sum of elements in c++ Program to find sum of elements in a given array --- sum of elements in c++ | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 sum of elements in c++ diff --git a/sum of elements in c++ b/sum of elements in c++ new file mode 100644 index 00000000..ed325ca5 --- /dev/null +++ b/sum of elements in c++ @@ -0,0 +1,18 @@ +#include +using namespace std; +int sum(int arr[], int n) +{ + int sum = 0; // initialize sum + for (int i = 0; i < n; i++) + sum += arr[i]; + + return sum; +} +int main() +{ + int arr[] = {12, 3, 4, 15}; + int n = sizeof(arr) / sizeof(arr[0]); + cout << "Sum of given array is " << sum(arr, n); + return 0; +} +