Display Multiplication Table in C#
What you’ll learn
- How a for loop prints times-table rows one by one.
- How to format rows like
n x i = resultwith string interpolation. - Two programs: a fixed table for 5 and a Console input version, plus a live preview.
Overview
A multiplication table is a perfect first loop exercise: the base number stays fixed, the multiplier runs from 1 to 10, and each line is one product. No arrays required — just repeated output.
Classic pattern
for (i = 1; i <= 10; i++) — ten rows.
Live preview
Change the base number and see the table instantly.
Reusable helper
PrintMultiplicationTable(base, last) works for any n.
Prerequisites
Basic for loops, integer multiplication, and Console.WriteLine.
- You know how a
forloop repeats a block a fixed number of times. - You can use
$"..."string interpolation to build readable output lines.
The idea
For a base number n, row i shows n × i. Print rows for i = 1 through 10 (or any limit you choose).
The loop variable is the multiplier; the product is number * i. That is the entire algorithm.
Sample rows for n = 5
5 × 1 = 5, 5 × 2 = 10, … up to 5 × 10 = 50. Each line adds 5 to the previous product when the multiplier increases by 1.
Live preview
Enter a base number and print the 1–10 times table in the browser.
Algorithm
Goal: print n × 1 through n × last.
Choose base
Use a fixed literal or read n from the user.
Loop multipliers
For i from 1 to last (often 10), compute n * i.
Print each row
Output n x i = product with WriteLine.
📜 Pseudocode
function printTable(n, last):
print header for n
for i from 1 to last:
print n, " x ", i, " = ", n * i Table for 5 (reference program)
Fixed base 5, multipliers 1 through 10 — matches the classic school times table and the old reference output.
using System;
class Program
{
static void PrintMultiplicationTable(int number, int last)
{
Console.WriteLine($"Multiplication Table for {number}:");
for (int i = 1; i <= last; i++)
{
Console.WriteLine($"{number} x {i} = {number * i}");
}
}
static void Main()
{
PrintMultiplicationTable(5, 10);
}
} Explanation
Extracting PrintMultiplicationTable with parameters number and last lets you reuse the same logic for any base — not only 5. The loop body is one multiply and one print.
Table for user input
Reads a positive integer from the console with int.TryParse, then prints the 1–10 table for that base.
using System;
class Program
{
static void PrintMultiplicationTable(int number, int last)
{
Console.WriteLine($"Multiplication Table for {number}:");
for (int i = 1; i <= last; i++)
{
Console.WriteLine($"{number} x {i} = {number * i}");
}
}
static void Main()
{
Console.Write("Enter a positive integer: ");
string? input = Console.ReadLine();
if (!int.TryParse(input, out int n) || n <= 0)
{
Console.WriteLine("Please enter a valid positive integer.");
return;
}
PrintMultiplicationTable(n, 10);
}
} Explanation
TryParse avoids crashes on bad input. Rejecting n <= 0 matches typical classroom rules; you could allow zero if the assignment says so.
Beyond the basics
Full grid. Nested loops can print a 10×10 table of products (Pythagoras table) — outer loop for rows, inner for columns.
while loop. Replace for with int i = 1; while (i <= last) { ... i++; } for the same behavior.
Interview: state the loop bounds, show one sample row on paper, then write the helper method.
❓ FAQ
🔄 Input / output examples
| Base (n) | Multiplier (i) | Row output |
|---|---|---|
5 | 1 | 5 x 1 = 5 |
5 | 10 | 5 x 10 = 50 |
3 | 7 | 3 x 7 = 21 |
12 | 12 | 12 x 12 = 144 (if last = 12) |
Edge cases and pitfalls
Validate input when the base comes from the user.
Non-numeric text
int.TryParse returns false — show a friendly message instead of throwing.
Zero or negative base
Math works, but many exercises expect positive n only.
last is zero
The loop prints nothing — valid but usually unintended; document expected range.
Very large n
n * i can overflow int for huge values — rare in beginner tasks.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Print k rows | O(k) | O(1) |
Each iteration does constant work — one multiply and one print.
Summary
- Pattern: loop
ifrom 1 tolast, printn x i = n*i. - C#:
forloop,$"..."interpolation, optionalTryParsefor input. - Complexity:
O(k)time forkrows,O(1)extra space.
A multiplication table for n is the list n×1, n×2, ... up to a limit. A simple for loop prints each row in O(k) time for k lines.
8 people found this page helpful
