English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What is the value of sum after the completion of the following statements?
int sum = 0;
for (int i=1; i<=6; i++) {
if ((i%2) == 0) {
sum = sum + i;
}
}


1. 6

2. 10

3. 12

4. 16

5. 21

2007-12-05 04:21:00 · 5 answers · asked by laraia87 1 in Computers & Internet Programming & Design

5 answers

The if condition allows the sum operation only for even values of i (0 == i modulo 2 for even values of i), and so, the sum is 2+4+6=12, given the range of i. Answer (3).

2007-12-05 04:30:20 · answer #1 · answered by richarduie 6 · 2 0

12

2007-12-05 12:31:27 · answer #2 · answered by ok_ranger 5 · 0 1

3. 12

i = 2
i = 4
i = 6

2007-12-05 13:19:49 · answer #3 · answered by CatNip 6 · 0 1

Actually it is none of the above. The answer is 42.

2007-12-05 12:27:07 · answer #4 · answered by Jim Maryland 7 · 2 3

the answer is 3.12

2007-12-05 12:32:06 · answer #5 · answered by sanchu@cse 2 · 0 1

fedest.com, questions and answers