fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using ll = long long;
  4. #define fo(i,start,end) for(int i = start;i<end;i++)
  5. using namespace std;
  6.  
  7. int main() {
  8. // your code goes here
  9. vector<int>arr = {3,4,1,2,1,4};
  10. int n = 6;
  11. int l = 2 ;
  12. int r = 5; //one based index
  13. int sum = 0;
  14. fo(i,l-1,r){
  15. sum += arr[i];
  16. }
  17. cout<<"Sum of array in range "<<l<<" to "<<r<<" is:"<<sum<<endl;
  18.  
  19. return 0;
  20. }
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
Sum of array in range 2 to 5 is:8