We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Warmup
- Simple Array Sum
- Discussions
Simple Array Sum
Simple Array Sum
+ 0 comments ** C ++ **
int simpleArraySum(vector<int> ar) { int sum = 0; for (int i=0; i<ar.size(); i++){ sum = sum + ar.at(i); } return sum; }
+ 0 comments Simple array sum |
C#
Common
internal interface ISolution { int SimpleArraySum(List<int> integers); }
Solution one
internal sealed class SolutionOne : ISolution { public int SimpleArraySum(List<int> integers) { int sum = default; foreach (int integer in integers) _ = sum += integer; return sum; } }
Solution two
internal sealed class SolutionTwo : ISolution { public int SimpleArraySum(List<int> integers) => integers.Sum(); }
Solution three
internal sealed class SolutionThree : ISolution { public int SimpleArraySum(List<int> integers) => integers.Aggregate(0, static (total, next) => total + next); }
+ 0 comments for java , we are getting input as list type array so our code will be a bit different than the simple array, code will be like this if you find any correction please let me know too.
public static int simpleArraySum(List<Integer> ar) { int sum = 0; for(int i=0; i<ar.size();i++) { sum+=ar.get(i); } return sum; }
+ 0 comments Bundle of thansk for sharing this. I am going to implement it on my webiste that is related to cleaning a septic tank.
+ 0 comments Very simple C++ code.
#include <iostream> using namespace std; int main() { int n,i; cin>>n; //limit of the array. int ar[n]; int sum = 0; for(i=0; i<n; i++){ cin>>ar[i]; } for(i=0; i<n; i++){ sum= sum+ar[i]; } cout<<sum; return 0; }
Load more conversations
Sort 3088 Discussions, By:
Please Login in order to post a comment