Skip to content
ExamSpine

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.

  1. Read the text on the first line.
  2. Read the count (whole number) on the second line.
  3. Print the text on its own line, repeated exactly count times (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

    1. typed: hello
    2. typed: 3

    It prints

    hello
    hello
    hello
  • one copy (rule 3)

    You type

    1. typed: test
    2. typed: 1

    It prints

    test
  • four copies (rule 3)

    You type

    1. typed: go
    2. typed: 4

    It prints

    go
    go
    go
    go
solution.pySolution.java