Sum Loop


Collapse Content

The remaining challenges will involve looping through arrays, so get ready!

Challenge

Print the sum of all even integers in the given array ar. (Ignore odd numbers and print each sum on its own line.)

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • how do I print the sum?

    public class Main{
    static void doStuff(int[] ar){
    for (i=1; i<ar.length; i++) {
    if (i%2==0) {
    System.out.println(sum ar);
    }
    }
    }

  • You need to use a variable to keep track of the sum, and print it at the end, after the loop.

  • int j=0;
    int n=ar.length;
    while(n>0){
    n=n-1;
    int m=ar[n];
    if(m%2==0){
    j=m+j;
    System.out.println(j);

    I'm not too sure why my code isn't working? Can I get a hint?

  • @Bernard, make sure you print the sum outside the loop. Also, to make things clearer, you could use a for loop instead of a while loop.

Contact Us
Sign in or email us at [email protected]