C Program to Generate a Multiplication Table
C Program to Generate a Multiplication Table
A school exercise asks you to multiplication table. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Generate each multiplication-table row by multiplying a fixed number by the changing row factor.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why multiplication table uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Print the first three rows for the number three.
Multiplication Table: follow the data
Given data
A school exercise asks you to multiplication table. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.
Rule
Generate each multiplication-table row by multiplying a fixed number by the changing row factor.
Observed result
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20
Understand the operation
The loop factor identifies the row. The displayed operands and product must come from the same iteration so a row cannot accidentally describe a different calculation.
Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int number = 4;
for (int i = 1; i <= 5; i++) printf("%d x %d = %d\n", number, i, number * i);
return 0;
}
Expected output
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int number = 4;
for (int i = 1; i <= 5; i++) printf("%d x %d = %d\n", number, i, number * i);
return 0;
}
The table number is four and factors begin at one.
Each iteration increments the factor and prints its product, through factor five.
Follow the worked calculation
Start with the sample
The table number is four and factors begin at one.
Apply the rule
Each iteration increments the factor and prints its product, through factor five.
Check the result
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int number = 4;
for (int i = 1; i <= 5; i++) printf("%d x %d = %d\n", number, i, number * i);
return 0;
}
Print the first three rows for the number three.
Worked answer
The worked answer follows the same rule with the requested changed data. Compare its complete source and output: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9. The result should follow from the operations, rather than from memorizing the first sample.
C17 · complete program
#include <stdio.h>
int main(void) {
int number = 3;
for (int i = 1; i <= 3; i++) printf("%d x %d = %d\n", number, i, number * i);
return 0;
}
Expected output
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
Run in C compiler →Recognize the important boundaries
The fixture deliberately stops at five. A general table should define a requested positive row count and ensure products fit.
The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Generate each multiplication-table row by multiplying a fixed number by the changing row factor.
Continue with arithmetic operand types.
Continue with validated input.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.