ritesh1340

Finding sum of all elements of an array

Dec 29th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector <int> v;
  5.  
  6. # define ll long long
  7.  
  8. ll indx(int n)
  9. {
  10.     if ( n==0 ) return v[0];
  11.     ll ans=v[n]+v[indx(n-1)];
  12.     return ans;
  13. }
  14.  
  15. int main()
  16. {
  17.     cout<<"Enter number of elements : ";
  18.     int n;
  19.     cin>>n;
  20.  
  21.     v=vector <int> (n, 1);
  22.  
  23.     for ( int i=0 ; i<n ; i++) cin>>v[i];
  24.  
  25.     ll ans=indx(n-1);
  26.     cout<<"Sum of all elements of the array : "<<ans;
  27.  
  28.     return 0;
  29. }
Add Comment
Please, Sign In to add comment