Daily C Language Challenge No. 4: Print the 9Ɨ9 Multiplication Table – Can You Optimize the Output Format?

šŸ“Œ Problem Description

Write a program to output a standard 9Ɨ9 multiplication table with the following requirements:

  • Present in astaircase alignment format

  • Allow the user to input n and output an nƗn multiplication table

  • Advanced: Implement with the least amount of code (testing code simplification ability)

Example output:

1Ɨ1=1  1Ɨ2=2   2Ɨ2=4  1Ɨ3=3   2Ɨ3=6   3Ɨ3=9  ...  1Ɨ9=9   2Ɨ9=18  ...  9Ɨ9=81  

Difficulty: ⭐(Basic problem, but formatting optimization is challenging).

šŸ’” Basic implementation (a must for beginners)

#include <stdio.h>
void print_table(int n) {    for (int i = 1; i <= n; i++) {        for (int j = 1; j <= i; j++) {            printf("%dƗ%d=%-4d", j, i, i*j); // -4d for left alignment        }        printf("\n");    }}
int main() {    print_table(9);    return 0;}

Key Points:

  • <span>%-4d</span>:Left alignment and fixed width of 4 characters

  • Inner loop<span>j <= i</span>: controls the number of items output per row

šŸ”„ Advanced optimization (minimal code version)

#include <stdio.h>int main() {    for (int i = 1, j; i <= 9; printf("\n"), i++)         for (j = 1; j <= i && printf("%dƗ%d=%-4d", j, i, i*j); j++);    return 0;}

Tips:

  • Utilize<span>printf</span> return value (returns the number of characters output successfully, non-zero is true)

  • Combine loop conditions and output to reduce the number of lines of code

⚔ User Interactive Version (Dynamic Size Adjustment)

#include <stdio.h>int main() {    int n;    printf("Input multiplication table size:");    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {        for (int j = 1; j <= i; j++) {            printf("%dƗ%d=%-6d", j, i, i*j); // dynamically adjust spacing        }        printf("\n");    }    return 0;}

Optimization Points:

  • <span>%-6d</span>: dynamically adjust spacing based on n (needs to increase when n≄10)

šŸŽØ Output Beautification Techniques

Requirement Implementation Method Effect Example
Right alignment <span>%4d</span> <span> 1→ 1</span>
Equal-width font alignment Tab character<span>\t</span> Automatically adapts length
Color output <span>\033[33m</span>(ANSI color code) Yellow text

Example of colored multiplication table:

printf("\033[33m%dƗ%d=%-4d\033[0m", j, i, i*j); // yellow output

Daily C Language Challenge No. 4: Print the 9Ɨ9 Multiplication Table - Can You Optimize the Output Format?Thought Question

How to implement an inverted triangle multiplication table(from 9Ɨ9 to 1Ɨ1)?

// Reference example
#include <stdio.h>
int main() {    int i = 9;      // Row number decreases from 9 to 1    int remaining = i;  // Number of elements remaining to output in the current row
    // There are a total of 45 items (9+8+7+...+1)    for (int k = 1; k <= 45; k++) {        int j = remaining;  // Column number decreases from the current remaining value to 1        printf("%d*%d=%-2d ", j, i, j * i);
        // At the end of each row, change line and reset row number and remaining value        if (j == 1) {            printf("\n");            i--;          // Row number decreases            remaining = i; // Reset remaining value to new row number        } else {            remaining--;  // Remaining value decreases        }    }    return 0;}

Code Analysis

  1. Single-layer loop design

  • Loop variable <span>k</span> iterates from <span>1</span> to <span>45</span> (inverted triangle has a total of 45 items).

  • <span>i</span> represents the row number (decreases from <span>9</span> to <span>1</span>),<span>remaining</span> represents the number of elements remaining to output in the current row.

  • Column number calculation

    • <span>j</span> starts from the current row’s <span>remaining</span> value and decreases, ensuring each row outputs <span>iƗi</span>, <span>iƗ(i-1)</span>, …, <span>iƗ1</span>.

  • Line break logic

    • When <span>j == 1</span>, it indicates the current row has been fully output, line break and decrease the row number <span>i</span>, while resetting <span>remaining</span>.

    4. Mathematical logic:

    Inverted triangle structure: The number of columns output per row decreases from the row number <span>i</span> to <span>1</span>, for example: Row 1:<span>i=9</span>, outputs <span>9Ɨ9</span> to <span>9Ɨ1</span> (9 items in total). Row 2:<span>i=8</span>, outputs <span>8Ɨ8</span> to <span>8Ɨ1</span> (8 items in total). And so on, until <span>i=1</span>, outputs <span>1Ɨ1</span> (1 item).

    // Example of running result
    9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9  8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8  7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7  6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6  5*5=25 5*4=20 5*3=15 5*2=10 5*1=5  4*4=16 4*3=12 4*2=8  4*1=4  3*3=9  3*2=6  3*1=3  2*2=4  2*1=2  1*1=1  

    šŸŽÆ Today’s Challenge

    Try to implement the multiplication table using a single-layer loop!šŸ’” Hint: Utilize the relationship between <span>i/j</span> and <span>i%j</span>.

    // Example code for single-layer loop implementation of 9Ɨ9 multiplication table
    #include <stdio.h>
    int main() {    for (int k = 1; k <= 81; k++) {        int i = (k - 1) / 9 + 1;  // Calculate row number i        int j = (k - 1) % 9 + 1;  // Calculate column number j
            // Only output when j <= i (ensuring lower triangular form)
            if (j > i) continue;
            printf("%d*%d=%-2d ", j, i, i * j); // Output item
            // Line break at the last element of each row
            if (j == i) printf("\n");    }    return 0;}

    Are there any other methods to implement it? Feel free to submit your code in the comments, and I will take time to review it.

    šŸš€ Next Issue Preview: No. 5: Input three side lengths to determine if they can form a triangle (and check if it is equilateral/isoceles/right-angled)

    šŸ“¢ Interaction Time

    1. What other variations of multiplication tables can you think of? Feel free to leave a comment!

    2. Vote: Which implementation do you prefer?

    • šŸ‘ Basic version (easy to read)

    • ā¤ļø Minimal version (show-off)

    • šŸ”„ Interactive version (practical)

    šŸš€ If you find it useful, feel free to share it with friends learningC language!

    Article Author:Vv Computer Graduate World (focusing on computer graduate exam tutoring for 8 years)

    Original Statement: Please contact for authorization to reprint, infringement will be pursued.

    Leave a Comment