Write a function with a while loop,
which merely prints the numbers 0..49 (stopping before 50).
Hint: What is a good name for your loop-control variable?
(An idiomatic, one-letter variable-name is great.)
for pedagogy only: Functions which print but don't return aren't typically of any use.
Modify the above to (print and) sum the numbers 0..49;
your function should return the sum.
Hint: What is a good name for your accumulator/“so-far” variable?
(Hint: the answer will be 49*50/2 = 1225.
But for this lab, use a loop, and not any formula.)
Modify the above to (print and) sum the numbers [0,n).
Hint: What is a good name for your parameter?
(An idiomatic, one-letter variable-name is great; it's mentioned in this very bullet. :-)
(One test-case you just made was which uses n=50.)
Make a second function to (print and) sum the numbers 0,3,6,9,…,48
(stopping before 50, as before).
(Hint: the answer should be 3*(16*17/2) = 4081)
Make a further function to (print and) sum the numbers 100,103,106,109,…,148 (stopping before 150).
(Hint: the answer you get should be 408 + 17*100.)
Finally: Make a function to (print and) sum the numbers in [a,b), counting by k.
This function should not print anything — just return the sum.
All previous functions are one-line calls to this most-general version.
Submit this last function on D2L as lab05a.
1 The 16 occurs because
⌊50/3⌋ = 50/3 = 16.
But we mention that just to note that there is a formula;
again, for this lab let's practice our loops!
↩