Sum Loop
Collapse Content
Show 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
Mandy
Oct 27, 4:58 AMhow 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);
}
}
}
Learneroo
Oct 27, 9:38 AMYou need to use a variable to keep track of the sum, and print it at the end, after the loop.
Bernard Mitchell
Jul 12, 3:35 PMint 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?
Learneroo
Jul 12, 7:11 PM@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.
thales
Jul 18, 12:27 PMa solution:
my code