






Hotspots: Admin Pages | Turn-in Site |
Current Links: Case Final Project Summer 2007
Q1 question and answer
Quiz # 1 — CS2340 Fall02
Write workspace code using appropriate control messages that will print out to the Transcript summation problems and their answers, as shown below. The successive lines show the summation of the digits from 1 to n for all n from 2 to 10, with each sum followed by ‘even’ or ‘odd’ corresponding to its value.
I want to see
1 + 2 = 3 odd
1 + 2 + 3 = 6 even
1 + 2 + 3 + 4 = 10 even
...
1 + 2 + 3 + 4 + 5 +6 + 7 + 8 + 9 + 10 = 55 odd
with the sums for n = 5 through 9 included, of course.
ANSWER
| sum |
2 to: 10 do: [:index |
Transcript cr; show: '1'.
sum := 1.
2 to: index do: [:n | Transcript show: ' + ', n printString. sum := sum + n].
Transcript show: ' = ', sum printString.
Transcript show: (sum odd ifTrue: [' odd'] ifFalse: [' even'])
]
Link to this Page