Simple Array Sum

Sort by

recency

|

3475 Discussions

|

  • + 0 comments

    Rust

    fn simple_array_sum(ar: &[i32]) -> i32 { let mut counter = 0;

    for i in ar {
        counter += i;
    }
    
    counter
    

    }

  • + 0 comments

    Simple and easy solution for C++

    int simpleArraySum(vector<int> ar) {
        return accumulate(ar.begin(), ar.end(), 0);
    }
    
  • + 0 comments

    For Python3 Platform

    I wrote the code from scratch to get more practice

    def simpleArraySum(arr):
        return sum(arr)
    
    n = int(input())
    arr = list(map(int, input().split()))
    
    result = simpleArraySum(arr)
    
    print(result)
    
  • + 0 comments

    in javascript is more simple :

    function simpleArraySum(ar) { return ar.reduce((arr,cur)=> arr + cur, 0)

    }

  • + 0 comments

    Example input If you want to sum 6 numbers:

    6
    1 2 3 4 5 6
    

    First line 6 → there are 6 integers in the array Second line → the actual numbers