Sum of Array
Sum of Array
Send Feedback
Sum of Array
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Sum
1 <= N <= 10^3
3
9 8 9
26
3
4 2 1
7
int sum(int input[], int n) { /* Don't write main(). Don't read input, it is passed as function argument. Return output and don't print it. Taking input and printing output is handled automatically. */if(n<0) return 0;int temp=sum(input,n-1);return temp+input[n-1];
}
#include<iostream>using namespace std;
int sum(int input[], int n) { /* Don't write main(). Don't read input, it is passed as function argument. Return output and don't print it. Taking input and printing output is handled automatically. */if(n<0) return 0;int temp=sum(input,n-1);return temp+input[n-1];
}
int main(){ int n; cin >> n; int *input = new int[n]; for(int i = 0; i < n; i++) { cin >> input[i]; } cout << sum(input, n) << endl;}
Comments
Post a Comment