手続 calcMod3 を呼び出したときの出力はどれか。 〔プログラム〕 ○calcMod3() 整数型: totalValue, i totalValue ← 0 for (i を 1 から 7 まで 1 ずつ増やす) if (i ÷ 3 の余り が 0 と等しい) totalValue ← totalValue + i endif endfor totalValue を出力する
令和7年度 ITパスポート試験 問78
This question's explanation and your answers so far.
問 78
Incorrectア
2 is neither one of the values that get added (3 and 6) nor their sum. Among 1 to 7 there are 2 multiples of 3, namely 3 and 6; mistaking this “count” for the answer gives 2, but what is output is not the count but the sum totalValue, so it is wrong.
Incorrectイ
3 is only the intermediate result right after the first addition at i=3. The loop continues all the way to i=7, and since i=6 is also a multiple of 3 it gets added too, so the final value is 9. If you stop accumulating at i=3, you would mistakenly choose 3.
Incorrectウ
7 is the loop's upper limit (the maximum value of i), not the sum. totalValue is the total of the i values that meet the condition, so the upper limit 7 is not output as-is.
- エ
正解 The for loop moves i from 1 to 7, increasing by 1 each time, and adds i to totalValue only when the condition “the remainder of i ÷ 3 equals 0” (i.e., i is a multiple of 3) holds. Among 1 to 7, the only multiples of 3 are 3 and 6, so totalValue = 3 + 6 = 9, and 9 is what is output at the end. Therefore the correct answer is エ “9”.
- “The remainder of X ÷ N is 0” means “X is a multiple of N.” In this problem, among 1 to 7 only the multiples of 3 (3 and 6) are the values to be added.
- The approach of finding the accumulated value by hand-tracing (desk-checking) a pseudocode loop that combines for with if (selection), substituting i one value at a time.