From 4d345121fd68db716aeae14c8e3c554eb3a630b6 Mon Sep 17 00:00:00 2001 From: Hardik9991 <73008712+Hardik9991@users.noreply.github.com> Date: Sat, 17 Oct 2020 10:46:08 +0530 Subject: [PATCH] sum of elements Program to find sum of elements in a given array --- sum of elements | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 sum of elements diff --git a/sum of elements b/sum of elements new file mode 100644 index 00000000..ed325ca5 --- /dev/null +++ b/sum of elements @@ -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; +} +