Integrating conditional arithmetic and structured loops transforms basic multiplication tables into visually appealing numerical grids
Pattern Title: Triangular Multiplication Table Pattern in C
Purpose: Boost your nested loop logic by generating matrix products dynamically based on row and column index relationships
Prerequisites: Familiarity with basic for loops, printf, scanf, and standard arithmetic operators
Final Output
For an input of rows = 5, the program generates
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Deconstructing the Pattern: The Logic
Problem Statement
Write a C program that prompts the user for the number of rows n and prints a triangular grid where the element at row i and column j represents the product of i \times j.
Pattern Analysis & Dynamic Logic
Rows (Outer Loop): Control variable
iruns from1ton(representing the base factor). Columns (Inner Loop): Control variable
jruns from1toi(representing the multiplier). Cell Calculation: Each printed value is computed dynamically as
i * j. Formatting: Proper spacing or tab formatting (
\tor%3d) ensures clean visual alignment regardless of single or multi-digit products.
Algorithm
Read the positive integer
nfrom the user. Execute an outer loop with variable
irunning from1ton. Execute an inner loop with variable
jrunning from1toi. Compute the product: {val} = i \times j.
Print
valformatted with consistent field width for alignment. Output a newline character (
\n) after completing the inner loop to end the row.
Code Implementation
int main() {
int n;
// Prompt user for input
printf("Enter the number of rows: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Please enter a valid positive integer.\n");
return 1;
}
// Outer loop for rows (base factor)
for (int i = 1; i <= n; i++) {
// Inner loop for columns (multiplier)
for (int j = 1; j <= i; j++) {
// Print product of i and j with width formatting for alignment
printf("%-4d", i * j);
}
// Move to the next line after completing the row
printf("\n");
}
return 0;
}
Line-by-Line Code Breakdown
if (scanf("%d", &n) != 1 || n <= 0): Performs strict input validation to handle non-numeric or non-positive integer values gracefully. for (int i = 1; i <= n; i++): Dictates row progression, settingias the multiplicand. for (int j = 1; j <= i; j++): Controls column generation up toiiterations, settingjas the multiplier. printf("%-4d", i * j);: Multipliesiandjdirectly and prints the result left-aligned in a 4-character wide field. printf("\n");: Moves the cursor to the next line to complete the triangular shape.
Compiling & Execution
Sample Run
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Variations & Enhancements
Full Matrix Grid: Change the inner loop condition to
j <= nto print a full $n \times n$ multiplication grid. Conditional Filter: Apply an
if-elseblock to print products only when they satisfy a condition (e.g.,if ((i * j) % 2 == 0)) and print spaces or symbols otherwise. Inverted Triangular Table: Reverse the outer loop (
for (int i = n; i >= 1; i--)) to output a descending multiplication triangle.
Common Mistakes & Troubleshooting
Alignment Misalignment: Using a plain space (
" ") causes grid distortion when products transition from 1-digit to 2-digit numbers. Use width specifiers (e.g.,%-4d) or tabs (\t). Confusing Factors: Swapping
i * jwith arbitrary variables can lead to incorrect sequence outputs. Missing Line Breaks: Omitting
printf("\n");outputs all calculated products onto a single continuous line.
Complexity Analysis
Time Complexity: {O}(n^2) — executing n(n+1)/2 total product calculations across nested loops
. Space Complexity: {O}(1) — runs in constant memory space
.
Mastering grid calculations and output formatting in nested loops builds strong foundational skills for complex multi-dimensional array operations
For all Pattern Programs list click here:
…till the next post, bye-bye & take care