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.
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
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