Thursday, September 10, 2026

Printing Multiplication Table Patterns in C | Conditional Patterns

Integrating conditional arithmetic and structured loops transforms basic multiplication tables into visually appealing numerical grids. Today’s article in our Pattern Programming in C series covers Multiplication Table Patterns. This tutorial demonstrates how to leverage nested loops to render both triangular and grid-based multiplication matrices.

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 i runs from 1 to n (representing the base factor).

  • Columns (Inner Loop): Control variable j runs from 1 to i (representing the multiplier).

  • Cell Calculation: Each printed value is computed dynamically as i * j.

  • Formatting: Proper spacing or tab formatting (\t or %3d) ensures clean visual alignment regardless of single or multi-digit products.

Algorithm

  1. Read the positive integer n from the user.

  2. Execute an outer loop with variable i running from 1 to n.

  3. Execute an inner loop with variable j running from 1 to i.

  4. Compute the product: {val} = i \times j.

  5. Print val formatted with consistent field width for alignment.

  6. Output a newline character (\n) after completing the inner loop to end the row.

Code Implementation

#include <stdio.h>

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, setting i as the multiplicand.

  • for (int j = 1; j <= i; j++): Controls column generation up to i iterations, setting j as the multiplier.

  • printf("%-4d", i * j);: Multiplies i and j directly 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

Enter the number of rows: 5
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 <= n to print a full $n \times n$ multiplication grid.

  • Conditional Filter: Apply an if-else block 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 * j with 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.

Try expanding this code to display only odd multiplication products while hiding even ones! Drop your code in the comments section below.


For all Pattern Programs list click here

…till the next post, bye-bye & take care