Multiplication Table
A counted loop can generate a predictable formatted table.
What is Multiplication Table?
A counted loop can generate a predictable formatted table.
Print a multiplication table for one number.
Important points
- Compile with warnings enabled and fix every warning.
- Know the lifetime and type of every value.
- Validate input and keep array bounds explicit.
C and C++ code examples
main.c
#include <stdio.h>
int main(void) {
int number = 7;
// Generate the first five rows.
for (int multiplier = 1; multiplier <= 5; ++multiplier)
printf("%d x %d = %d\n", number, multiplier, number * multiplier);
}
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
C++
main.cpp
#include <iostream>
int main() {
int number = 7;
// Generate the first five rows.
for (int multiplier = 1; multiplier <= 5; ++multiplier)
std::cout << number << " x " << multiplier << " = " << number * multiplier << '\n';
}
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
C and C++ comparison
The loop counter supplies each multiplier and the product is calculated for display.
C
C exposes small procedural APIs and makes representation details explicit.
C++
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.