C - Write program to generate a multiplication table

Requirements

Write program to generate a multiplication table

A table of size 4 would have four rows and four columns.

The rows and columns would be labeled from 1 to 4.

Each cell in the table will contain the product of the corresponding row and column numbers.

Demo

#include <stdio.h>

int main(void)
{
  int table_size = 6;                       // Table size        */

  for (int row = 0; row <= table_size; ++row)
  {//from w w  w.  j av  a  2 s  .c o  m
    printf("\n");                      // Start new row
    for (int col = 0; col <= table_size; ++col)
    {
      if (row == 0)                     // 1st row?
      {                                // Yes - output column headings
        if (col == 0)                   // 1st column?
          printf("    ");              // Yes - no heading
        else
          printf("|%4d", col);         //output heading
      }
      else
      {                                // Not 1st row - output rows
        if (col == 0)                   // 1st column?
          printf("%4d", row);          // Yes - output row label
        else
          printf("|%4d", row*col);     // No - output table entry
      }
    }
    if (row == 0)                      // If we just completed 1st row
    {                                  // output separator dashes
      printf("\n");
      for (int col = 0; col <= table_size; ++col)
        printf("_____");
    }
  }
  printf("\n");
  return 0;
}

Result

Related Exercise