Multiplication Table Grid - Problem

Generate an N×N multiplication table and display it in a neatly aligned grid format with row and column headers.

The table should show the product of each row number (1 to N) with each column number (1 to N). Format the output so that all numbers are properly aligned in columns, making it easy to read.

For example, a 4×4 multiplication table would show products from 1×1 to 4×4 arranged in a grid with headers.

Input & Output

Example 1 — Small Table
$ Input: n = 3
Output: 1 2 3 1 1 2 3 2 2 4 6 3 3 6 9
💡 Note: A 3×3 multiplication table with row and column headers. Each cell shows the product of row number × column number, properly aligned.
Example 2 — Larger Table
$ Input: n = 4
Output: 1 2 3 4 1 1 2 3 4 2 2 4 6 8 3 3 6 9 12 4 4 8 12 16
💡 Note: A 4×4 multiplication table. Notice how larger numbers (like 16) maintain proper column alignment throughout the grid.
Example 3 — Minimal Case
$ Input: n = 1
Output: 1 1 1
💡 Note: The smallest possible multiplication table: just 1×1 = 1 with proper headers and formatting.

Constraints

  • 1 ≤ n ≤ 12
  • Output must be properly aligned in columns
  • Include row and column headers

Visualization

Tap to expand
INPUTALGORITHMRESULTN = 4Table SizeGenerate 4×4multiplication tablewith headers1Calculate max width: 16 → 2 digits2Build header: " 1 2 3 4"3For each row i:Calculate i×j products4Format with proper spacingFormatted Table 1 2 3 4 1 1 2 3 4 2 2 4 6 8 3 3 6 9 12 4 4 8 12 16Perfect Alignment!Key Insight:Calculate maximum column width first, then use consistent formatting for perfect grid alignmentTutorialsPoint - Multiplication Table Grid | Row-by-Row Generation
Asked in
Microsoft 15 Apple 12 IBM 8
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen