Lesson 4.2 · Write it
Show N times
A whole programPython or Java6 tests, 3 of them hidden
On a phone? Put the lines in order instead
Write a program that reads a line of text and a number, then prints that text exactly that many times.
- Read the text on the first line.
- Read the count (whole number) on the second line.
- Print the text on its own line, repeated exactly
counttimes (one copy per line).
Define a function that takes the text and the count. The count is a whole number from 1 to 100. Your program is run several times. Only what it prints is checked.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
three copies of hello (rule 3)
You type
- typed: hello
- typed: 3
It prints
hello hello hello
one copy (rule 3)
You type
- typed: test
- typed: 1
It prints
test
four copies (rule 3)
You type
- typed: go
- typed: 4
It prints
go go go go