Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

Wrte a for loop to compute the sum 1 + 2 + 3 + 4 Multiple answers. Answer A int sum =0; for (int j = 0 ; j < 4 ; j++) sum=sum+j; B int sum =0; for (int j = 1; j < 5 ; j++) sum=sum+j; C int sum =0; for (int j = 1 ; j < 6 ; j++) sum=sum+j; D int sum =0; for (int j = 1 ; j <= 4 ; j++) sum += j;

OpenStudy (anonymous):

B

OpenStudy (anonymous):

sum = 0 outside loop. ( initial value j=1; ending condition j<5; increment by 1 each round of lopp j++) sum = sum + j. so on round 1 sum=0+ 1 = 1 so on round 2 j= 2 not 1. so sum = 1 + 2 =3 (not 2 as is in the series needed) Is that a problem denyekwe?

OpenStudy (anonymous):

d

OpenStudy (anonymous):

B and D work

OpenStudy (anonymous):

@arcticf0x why would you think D is wrong? it iterates 4 times because <=

OpenStudy (anonymous):

Uh oh, overlooked, D is correct aswell. @Tomas.A

OpenStudy (anonymous):

So B and D.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!