Skip to main content

Exercises 22.10 Chapter Exercises

1.

Complete the code to create a program that adds up all the numbers from 10 to 20. Make sure to include both 10 and 20 in the sum.

2.

Complete the code to calculate the sum of the squares of all numbers from 1-10 (inclusive). The sum of squares is the result of squaring each value before adding it to the sum. The sum of squares for the numbers 1-4 would be \(1 * 1 + 2 * 2 + 3 * 3 + 4 * 4 = 30\text{.}\)
It might help to calculate each square and print it out before adding it to the sum to make sure you are using the right values.

3.

The following program calculates the height of an object as it falls from 1000 feet. The program should use a loop to print out the current height, then update the time by one second and calculate a new height. It should stop looping once the object reaches the ground (height 0) and then print out the final number of seconds.
Put the blocks in the right order and indentation. As usual, you will not use all the blocks.

4.

To count how many digits there are in a number, you can do the following:
while the number is greater than 0
    set the number to the number divided by 10 (ignore any remainder)
    add one to the number of digits
Below is a function digitsIn that is supposed to calculate the number of digits in A given number. You need to write the logic to calculate that.
Hint: when you divide number by 10, make sure to use // to do whole number division.
Remember the debugging tricks you used - use codelens or print out values to make sure your work is correct.

5.

Below is the start of a program to calculate how long it will take to have at least $50,000 if you invest $300 per month at 0.5% interest per month (approximately 6% per year).
You should add code to continue doing the following until money reaches 50000:
  • Increase the existing money by 0.5% (multiply it by 1.005). (This is interest earned on all money already invested.)
  • Add the monthly investment amount to money. (This is another month of investment.)
  • Increase the months count by 1
Remember the debugging tricks you used - use codelens or print out values to make sure your work is correct.
You have attempted of activities on this page.