Skip to lab content

UNIT 01 · Simple numeric problems

Multiplication table with a chosen row count

EXERCISE 01CC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a program that prints a multiplication table for a given number and the number of rows in the table. For example, for a number 5 and rows = 3, the output should be: 5 x 1 = 5; 5 x 2 = 10; 5 x 3 = 15.
Input format & conventions

An integer number (-1,000,000 to 1,000,000), then the row count (1 to 1000).

UNDERSTAND THE IDEA

Explanation

The multiplier starts at 1 and increases until the requested row count. Each iteration prints number × multiplier.

The row count is an input, so the solution does not assume that every table has ten rows.

PLAN BEFORE CODING

Algorithm

  1. Read the number and row count.
  2. Reject a row count outside 1–1000.
  3. Loop from 1 through the requested number of rows.
  4. Print one formatted multiplication per iteration.

SEE THE CONTROL FLOW

Flowchart

Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.

Flowchart for Multiplication table with a chosen row count: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#include <stdio.h>

int main(void) {
    long long number;
    int rows;
    if (scanf("%lld %d", &number, &rows) != 2 ||
        number < -1000000 || number > 1000000 || rows < 1 || rows > 1000) {
        puts("Invalid input.");
        return 1;
    }
    for (int i = 1; i <= rows; ++i)
        printf("%lld x %d = %lld\n", number, i, number * i);
    return 0;
}
Open in compiler ↗

Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.

Compile and run locally
gcc -std=c17 multiplication-table.c -o lab
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
i = 15 × 15
i = 25 × 210
i = 35 × 315

CHECK THE BEHAVIOR

Sample input & output

Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.

Sample 1

INPUT
5 3
OUTPUT
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15

Sample 2

INPUT
-2 4
OUTPUT
-2 x 1 = -2
-2 x 2 = -4
-2 x 3 = -6
-2 x 4 = -8

Sample 3

INPUT
0 1
OUTPUT
0 x 1 = 0

Common mistakes

  • Use i <= rows to include the final row.
  • Do not replace the entered row count with a fixed constant such as 10.

WHY THIS GROWTH RATE?

Time and space complexity

Time O(rows); auxiliary space O(1).

Let r be the requested number of rows. The loop runs exactly r times. Every iteration multiplies two fixed-width integers and prints one row.

The work is proportional to r: T(r) = c × r + k, so time is O(r). Doubling the row count approximately doubles the work. Only the number, counter and row limit are stored, giving O(1) auxiliary space.

Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.