-
If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.
-
You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!
|
Exercises - Intro to Counted Loops
Page history
last edited
by Steve Sweeney 8 years, 7 months ago
The following exercises should be named "counted01a", "counted03b", etc.
- (a) Ask the user their name and output the name 5 times. (turing solution)
Extension:
(b) Allow the user to specify the number of outputs. (turing solution)
Hold down mouse and scroll over hints to highlight them and make them visible
Hint 1 |
Hint 2 |
Hint 3 |
for count : 1 .. 5
|
Ask the user for an integer value (N) |
for count : 1 .. N |
- (a) Output the times-tables (from 1 to 12) for whichever number the user requests. For example, if the user enters 3, your output should be:
1 x 3 = 3
2 x 3 = 6
...
12 x 3 = 36
Hint: See the Output in Turing page for a more compact (efficient) way to "put" multiple values on the same line. (turing solution)
Extension:
(b) Allow the user to specify the start and end to the table (e.g., 4 to 15); (turing solution)
(c) Allow the user to specify the step size (e.g., by 3 is 4, 7, 10, 13).
Hint 1 |
Hint 2 |
Hint 3 |
Need a for loop from 1 to 12 |
Use a "put" statement inside the for loop to display your answer |
Combine "put" statements using the ".." at the end of each line; you need to output (a) the user's number, (b) the current counter, and (c) the product of userNum*counter |
- (a) Write a program which will ask the user how many symbols to output, and then output that many lines of symbols (one symbol on each line). To start, use the '*' symbol. (Turing Solution)
(b) Write a program which will ask the user how many symbols to output, and then it will output that many symbols on one line. To start, use the '*' symbol. For example, if the user enters 5, the output would be: ***** (c) Output the user's number as well as the symbols, all on the same line. For example, if the user entered a value of 10, the output would be: 10 ********** (d) Modify your program to allow the user to choose a symbol as well. Number of symbols? 7 Symbol to use? @ 7 @@@@@@@ (e) Allow the user the specify the total number of symbols and how many to print on each line. For example, if the user entered 10 symbols with 4 per line, the output might be: $$$$ $$$$ $$ (the last line only has two symbols because we only print 10 total)
- (a) Output the numbers from 1 to 10 both numerically (i.e., “1”, “2”, etc.) and graphically (using the character of your choice).
For example, output might look something like this:
1 *
2 **
3 ***
4 ****
Hint: You may want to look at methods for combining strings here (Output in Turing) (Turing Solution)
Extension:
(b) Allow the user to specify the start and end numbers of the range.
(c) Allow the user to specify the symbol to be used in building the graphical component (e.g., *, +, x, #, @) (d) Modify your program to print the pattern horizontally instead of vertically. For example, the output might look like:
* ** *** ****
- (a) Ask the user to enter 5 integer values between 1 and 50. Create a simple bar graph using the character of your choice.
Hint: You may want to try to use a loop within a loop (a nested loop). Don't forget to indent properly!
For example: If the user were to enter 3, 1, 6, 2, 5, the output might look like:
###
#
######
##
#####
Extension:
(b) Let the user specify the number of values;
(c) Let the user enter any values they want, and scale your graph so it fits on the screen. (d) [challenge] Make the bar graph horizontal
-
(a) [Challenge] Ask the user to input a number between 1 and 99, and then tell the user how many "tens" and how many "ones" in their number.
For example, if the user entered 75:
75 has 7 tens and 5 ones
Extension:
(b) Increase the scope of the program to include "hundreds" and allow inputs from 1 to 999
(c) Improve your output to avoid any grammatical errors (i.e., say "11 has 1 ten and 1 one", not "1 has 1 tens and 1 ones")
(d) Further improve your output to avoid any "zero" statements (i.e., say "7 has 7 ones", not "7 has 0 tens and 7 ones")
(e) Let the user specify how many numbers they will enter.
Hint: you will probably want to have one number entry per line, and you will break down each number as they enter it.
see More Exercises with Counted Loops
Exercises - Intro to Counted Loops
|
Tip: To turn text into a link, highlight the text, then click on a page or file from the list above.
|
|
|
|
|
Comments (0)
You don't have permission to comment on this page.