Practice Problems  Loops / Repetition StructuresThese problems will help you learn to use various programming concepts.

Intro to Turing (2014)
Practice Problems
1. Write a program containing a counted loop (that repeats 20 times) which outputs the series of integers starting at 5 and going up by 5s. Revise this program to output the integers starting at 5 and decreasing by 10s until you get to 35.
2. Write a program that tells you to "Have a good day" twenty times.
3. Write a program that reads words entered one to a line and counts how many words have been entered before you give the signal word "end" to stop execution of the program. Experiment to determine what happens if you put several words on a line as you enter them.
4. ICS2O DO NOT DO THIS QUESTION!!! A series of marks will be entered and the average calculated. Before you enter the series you are to have the program ask you how many marks there are in the series then read it in. Test your program to see that it works for series of different lengths, say four marks or six marks.
6.aWrite a program to output a table of values of the integers starting at 1 and their squares. Label the table at the top of the columns. For example, your output might look like this
Number Square
1 1
2 4
3 9
4 16
5 25
Try to format the output so that it looks attractive. What happens as the numbers get larger and larger? Change the program to output the first 100 integers rather than attempting to go on forever.
6.b Modify your program so that in one 'for' loop you output the following
Num Square Num Square Num Square Num Square
1 1 21 441 41 1681 61 3721
2 4 22 484 42 1764 62 3864
continue through to
20 400 40 1600 60 3600 80 6400
7. Write a program using a loop counting backwards. Output the index of the loop on each execution so the output is the same as the count down for a rocket launch. Arrange the output so that it is all on one line like this
5 4 3 2 1
8. Write a program to output a backwards count by 5s from 100 down to 5. Modify it so that you count from 100 down to 50.
Modify it so that before you start the count you can input a number between 100 and 50 so that the program will stop when the count would be less than the number input. For example the execution might be like this:
What number do I stop at? 82 Stop when count less than 82
100
95
90
85
9. Write a program to find the sum of a number of terms of the infinite series 1 + x + x**2 + x**3 + x**4 + ...where the number of terms n to be evaluated and the value of
x are input before the summation begins. Experiment with different values of n and x.
10.Write a program to compute the bank balance at the end of each year for 10 years resulting from an initial deposit of $1000 and an annual interest rate of 6%. Output for each year end the number of the year, the initial balance, the interest for the year, and the balance at the end of the year.
11.a A homeowner takes out a mortgage for $120,000 at 7.75% per year. At the end of each year an amount of $24,000 is paid. Write a program to show how the mortgage is paid off, year by year, until nothing is owing.
11.b Assume that each month in the year has 30.5 days in it. Give the number of the month and the day in the month in
which the mortgage is paid. (i.e. month #3, day #20)
12.Write a program to simulate the playing of a simple dice game (played with one die). Roll the die to get a value from 1 to 6.
This we will call your point. Now keep rolling until you get the same value (your point) again and see how many rolls it
takes. Program it so you can play this game repeatedly.
13. Ask the user for an integer between 1 and 50. Output all the factors of that integer. Next, modify the program so that it outputs the factors of each integer up to the value of the integer input by the user.
14. Ask the user for an integer. Output the number of digits in the integer. Then output the sum of the digits.(i.e. 1234 has 4 digits and their sum is 10).
15.Write a program to keep inputting integers until a perfect square (for example 64) between 40 and 100 is entered. (This is a difficult one!)
16.Write a program to generate 10 random real numbers
between:
a. 4 and 5
b. 0 and 10
c. 20 and 30
d. x and y where x and y are integer inputs.
2. Write a program that tells you to "Have a good day" twenty times.
3. Write a program that reads words entered one to a line and counts how many words have been entered before you give the signal word "end" to stop execution of the program. Experiment to determine what happens if you put several words on a line as you enter them.
4. ICS2O DO NOT DO THIS QUESTION!!! A series of marks will be entered and the average calculated. Before you enter the series you are to have the program ask you how many marks there are in the series then read it in. Test your program to see that it works for series of different lengths, say four marks or six marks.
6.aWrite a program to output a table of values of the integers starting at 1 and their squares. Label the table at the top of the columns. For example, your output might look like this
Number Square
1 1
2 4
3 9
4 16
5 25
Try to format the output so that it looks attractive. What happens as the numbers get larger and larger? Change the program to output the first 100 integers rather than attempting to go on forever.
6.b Modify your program so that in one 'for' loop you output the following
Num Square Num Square Num Square Num Square
1 1 21 441 41 1681 61 3721
2 4 22 484 42 1764 62 3864
continue through to
20 400 40 1600 60 3600 80 6400
7. Write a program using a loop counting backwards. Output the index of the loop on each execution so the output is the same as the count down for a rocket launch. Arrange the output so that it is all on one line like this
5 4 3 2 1
8. Write a program to output a backwards count by 5s from 100 down to 5. Modify it so that you count from 100 down to 50.
Modify it so that before you start the count you can input a number between 100 and 50 so that the program will stop when the count would be less than the number input. For example the execution might be like this:
What number do I stop at? 82 Stop when count less than 82
100
95
90
85
9. Write a program to find the sum of a number of terms of the infinite series 1 + x + x**2 + x**3 + x**4 + ...where the number of terms n to be evaluated and the value of
x are input before the summation begins. Experiment with different values of n and x.
10.Write a program to compute the bank balance at the end of each year for 10 years resulting from an initial deposit of $1000 and an annual interest rate of 6%. Output for each year end the number of the year, the initial balance, the interest for the year, and the balance at the end of the year.
11.a A homeowner takes out a mortgage for $120,000 at 7.75% per year. At the end of each year an amount of $24,000 is paid. Write a program to show how the mortgage is paid off, year by year, until nothing is owing.
11.b Assume that each month in the year has 30.5 days in it. Give the number of the month and the day in the month in
which the mortgage is paid. (i.e. month #3, day #20)
12.Write a program to simulate the playing of a simple dice game (played with one die). Roll the die to get a value from 1 to 6.
This we will call your point. Now keep rolling until you get the same value (your point) again and see how many rolls it
takes. Program it so you can play this game repeatedly.
13. Ask the user for an integer between 1 and 50. Output all the factors of that integer. Next, modify the program so that it outputs the factors of each integer up to the value of the integer input by the user.
14. Ask the user for an integer. Output the number of digits in the integer. Then output the sum of the digits.(i.e. 1234 has 4 digits and their sum is 10).
15.Write a program to keep inputting integers until a perfect square (for example 64) between 40 and 100 is entered. (This is a difficult one!)
16.Write a program to generate 10 random real numbers
between:
a. 4 and 5
b. 0 and 10
c. 20 and 30
d. x and y where x and y are integer inputs.