Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find and display the Multiplication Table in C#?
A multiplication table displays the product of a number with a sequence of other numbers. In C#, you can generate and display multiplication tables using loops and formatted output. This is useful for educational applications, mathematical calculations, or creating reference tables.
Syntax
The basic structure for creating a multiplication table uses a loop with formatted output −
while (counter <= limit) {
Console.WriteLine("{0} x {1} = {2}", number, counter, number * counter);
counter++;
}
You can also use a for loop for more concise code −
for (int i = 1; i <= limit; i++) {
Console.WriteLine("{0} x {1} = {2}", number, i, number * i);
}
Using While Loop
The while loop approach gives you control over the increment and allows for custom conditions −
using System;
public class Demo {
public static void Main() {
int n = 4, a = 1;
Console.WriteLine("Multiplication Table of " + n);
Console.WriteLine("------------------------");
while (a <= 10) {
Console.WriteLine("{0} x {1} = {2}", n, a, n * a);
a++;
}
}
}
The output of the above code is −
Multiplication Table of 4 ------------------------ 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 4 x 10 = 40
Using For Loop
A for loop provides a more compact solution for generating multiplication tables −
using System;
public class Demo {
public static void Main() {
int number = 7;
Console.WriteLine("Multiplication Table of " + number);
Console.WriteLine("------------------------");
for (int i = 1; i <= 12; i++) {
Console.WriteLine("{0} x {1} = {2}", number, i, number * i);
}
}
}
The output of the above code is −
Multiplication Table of 7 ------------------------ 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 7 x 11 = 77 7 x 12 = 84
Multiple Tables at Once
You can also generate multiple multiplication tables using nested loops −
using System;
public class Demo {
public static void Main() {
Console.WriteLine("Multiplication Tables (1 to 3)");
Console.WriteLine("==============================");
for (int table = 1; table <= 3; table++) {
Console.WriteLine("\nTable of " + table + ":");
for (int i = 1; i <= 5; i++) {
Console.WriteLine("{0} x {1} = {2}", table, i, table * i);
}
}
}
}
The output of the above code is −
Multiplication Tables (1 to 3) ============================== Table of 1: 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 Table of 2: 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 Table of 3: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15
Comparison of Approaches
| Approach | Best Use Case | Advantages |
|---|---|---|
| While Loop | Custom conditions or complex logic | More control over loop execution |
| For Loop | Simple sequential tables | Cleaner, more concise code |
| Nested Loops | Multiple tables or grids | Generates multiple tables efficiently |
Conclusion
Generating multiplication tables in C# is straightforward using loops and formatted output with Console.WriteLine(). Choose while loops for custom conditions, for loops for simple sequential tables, and nested loops when generating multiple tables at once.
