Basic Product Table in C

Multiplication Table in C

A basic Math operation like multiplication is probably elementary. I have been playing around with the "OG" language, the C programming language. Here, I try to use 2-D arrays in C, and add a bit of color-coded output, to make mine a bit different. So this is my procedure where I learn other intricates of C language while creating a basic multiplication table. Math and programming can co-exist! Here is the code, you can compile using the online tool here:

/******************************************************************************
                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#define TSIZE 15
#define ANSI_COLOR_RED     "\x1b[31m"
#define ANSI_COLOR_GREEN   "\x1b[32m"
#define ANSI_COLOR_YELLOW  "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[42m"
#define ANSI_COLOR_BLUE_BACKGROUND "\x1b[44m"
int main()
{
    int c;
    int sum = 0;
    int mdigit[TSIZE][TSIZE];
    for(int r=0; r<TSIZE; r++){
        for(int c=0; c<TSIZE; c++){
            mdigit[r][c]= (r+1) * (c+1);
        }
    }

    // Print column headers
    printf(ANSI_COLOR_BLUE_BACKGROUND"      ");
    for(int i = 0; i <TSIZE; i++) {
        printf( ANSI_COLOR_GREEN "x %2d |", i+1);
    }
    printf("\n");

    for(int r=0; r<TSIZE; r++){
        // print the row headers
        printf(ANSI_COLOR_RED "%4d |", r+1);
        // print r*c
        for(int c=0; c<TSIZE; c++){
            printf(ANSI_COLOR_YELLOW "%4d |",mdigit[r][c]);
        }
        printf("\n");
    }

    return 0;
}

In the above code (its complete and runs in the compiler) I utilize ANSI escape codes for color styling, similar to the way UNIX files and directories are highlighted using different colors in the terminal.

In the above case, I have 15 rows, for a 15 by 15 multiplication table. You can also change to the desired row size by adjusting the macro in the full code:

#define TSIZE 15 // <-- change this number value to your preferred.

Checkout the output in the image below. I think it is pretty cool. Multiplication Table in C

ANSI Codes for Color

Specify the in the pre-processor directive ANSI_COLOR_GREEN value below:

\x1b[32m

This will provide our output with a green color for the column headers.

Pre-processor Directives

A nice way to create constants in the C language is through pre-processor directives. Moreover, the red and yellow highlighting is given by the codes "\x1b[31m", and "\x1b[33m", which are accessed easily with their respective pre-processor directives. The background is also provided using the directive ANSI_COLOR_BLUE_BACKGROUND set to the code value below:

\x1b[44m

The following references helped:

References

  1. https://stackoverflow.com/questions/3219393/stdlib-and-colored-output-in-c
  2. https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

links

social