Sum Loop


Premium Content - Free Preview

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


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

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]