C# Program to Print Multiplication Table
You can print a multiplication table in C# using a
for loop like this: for (int i = 1; i <= 10; i++) { Console.WriteLine($"{number} x {i} = {number * i}"); } where number is the base number for the table.Examples
Inputnumber = 2
Output2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Inputnumber = 5
Output5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Inputnumber = 0
Output0 x 1 = 0
0 x 2 = 0
0 x 3 = 0
0 x 4 = 0
0 x 5 = 0
0 x 6 = 0
0 x 7 = 0
0 x 8 = 0
0 x 9 = 0
0 x 10 = 0
How to Think About It
To print a multiplication table, think of counting from 1 to 10 and multiplying each count by the chosen number. For each step, you show the multiplication expression and its result. This repeats until you reach 10.
Algorithm
1
Get the number for which to print the multiplication table2
Start a loop from 1 to 103
Multiply the number by the current loop count4
Print the multiplication expression and result5
Repeat until the loop reaches 10Code
csharp
using System; class Program { static void Main() { int number = 7; for (int i = 1; i <= 10; i++) { Console.WriteLine($"{number} x {i} = {number * i}"); } } }
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Dry Run
Let's trace the multiplication table for number 3 through the code
1
Set number
number = 3
2
Start loop
i = 1
3
Calculate and print
3 x 1 = 3
4
Increment loop
i = 2
5
Calculate and print
3 x 2 = 6
| i | Output |
|---|---|
| 1 | 3 x 1 = 3 |
| 2 | 3 x 2 = 6 |
| 3 | 3 x 3 = 9 |
| 4 | 3 x 4 = 12 |
| 5 | 3 x 5 = 15 |
| 6 | 3 x 6 = 18 |
| 7 | 3 x 7 = 21 |
| 8 | 3 x 8 = 24 |
| 9 | 3 x 9 = 27 |
| 10 | 3 x 10 = 30 |
Why This Works
Step 1: Loop from 1 to 10
The for loop runs 10 times, once for each multiplier from 1 to 10.
Step 2: Multiply and print
Inside the loop, the program multiplies the base number by the current loop index and prints the result.
Step 3: Use string interpolation
The $"{number} x {i} = {number * i}" syntax creates a readable output showing the multiplication expression and result.
Alternative Approaches
Using while loop
csharp
using System; class Program { static void Main() { int number = 4; int i = 1; while (i <= 10) { Console.WriteLine($"{number} x {i} = {number * i}"); i++; } } }
This uses a <code>while</code> loop instead of <code>for</code>, which is more flexible but slightly longer.
User input for number
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int number = int.Parse(Console.ReadLine()); for (int i = 1; i <= 10; i++) { Console.WriteLine($"{number} x {i} = {number * i}"); } } }
This version asks the user to enter the number, making the program interactive.
Complexity: O(1) time, O(1) space
Time Complexity
The loop runs exactly 10 times, so the time is constant, O(1), regardless of the input number.
Space Complexity
The program uses a fixed amount of memory for variables and output, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time; differences are mainly in code style and user interaction.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop | O(1) | O(1) | Simple, clear fixed-length loops |
| While loop | O(1) | O(1) | Flexible loop conditions |
| User input | O(1) | O(1) | Interactive programs |
Use string interpolation with
$"..." for clear and easy output formatting.Beginners often forget to update the loop counter, causing infinite loops or no output.