Back to Challenge Set 9

Code Challenge: Times Table

Difficulty  

divider

The Brief

What they asked for
I tutor my little cousin and she is stuck on her times
tables. Ask me which one she is working on, then print that
whole table from one to twelve, one line each, written out
the way it is on her worksheet so she can check herself.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • You know how many lines this prints before you start. Twelve, every single time, whatever number she is working on. That is what decides which loop you reach for.
  • Two different numbers are in play — the one she asked about, and the one the loop is counting. Only one of them changes each pass.
  • Read the sample output before you write the line. The worksheet writes it a particular way round, and matching it is part of the job.
  • Decide what should happen if somebody enters 0, or a negative number. Write your decision down, then check whether your program actually does that.

Where to Start

Only one-star challenges have this section. If you already know how to begin, skip it — and when you move up to two stars, working out the order yourself is part of the job.

  1. Ask which number she is working on, and convert the answer.
  2. Print the heading line.
  3. Write a counted loop that runs from 1 to 12.
  4. Inside the loop, work out the answer for this pass and display the whole line.

Sample Output

Sample Output
Which number? 7 [Enter]
Times table for 7
1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35
6 x 7 = 42
7 x 7 = 49
8 x 7 = 56
9 x 7 = 63
10 x 7 = 70
11 x 7 = 77
12 x 7 = 84

Test 7, then 1, then 0. All three should print twelve lines — including 0, which is a boring table but a real one.


Submit

Your requirements list and your program file, named cc-times-table.

Commence Challenge