Given a integer array we have to find the sum of element in an array. .
Example:
input:arr[]={1,2,3,4,5}
output:15
Logic:
step 1- iterate the whole array and initialise sum variable.
step 2-add the value to the sum variable
Code-
#include<bits/stdc++.h> using namespace std; int main() { int arr[] = { 22, 85, 11, 90, 84, 12 }; int n = sizeof(arr) / sizeof(arr[0]); int sum=0; for(int i=0;i<n;i++) { sum+=arr[i]; } cout<<sum<<endl; }
Output:
304
Time complexity:0(N)
Space Complexity:O(1)