Search code examples
javaarraysbluej

How do I return the sum of an array, with this code to start it off?


So this is what I have:

public static void Positive () {
    int limit = 50;
    for(int i=1; i <= limit; i++){
        System.out.print(i + ", ");
    }
}

That prints out all numbers 1-50 with a loop

How do I return the sum of this using a loop?


Solution

  • Here is what you can do

    public static void Positive () {
    int limit = 50;
    int total = 0;
    for(int i=1; i <= limit; i++){
        total = total + i;
       }
    System.out.print(total);
    }