Write A C# Program To Print The Multiplication Table Of A Number

Do you want to know how to print the multiplication table of a number in C#? In this tutorial, I will explain how to write a C# program to print the multiplication table of a number.

To create a C# program for printing a multiplication table of a given number, start by setting up a C# console application environment. Inside the Main method, use Console.ReadLine() to take user input, then employ a for loop to iterate and print the multiplication table for that number, using Console.WriteLine() for output.

C# Program To Print The Multiplication Table Of A Number

I will show you how to write a C# program that generates the multiplication table for a given number. The program will prompt the user to enter a number and then generate and display the multiplication table for the entered number.

For example, if the user inputs the number 3, our program should output the multiplication table of 3 (up to a certain limit, say 10):

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
3 x 10 = 30

Here, we will write a console application to print the multiplication table of a number in C#.

Step 1: Setting Up the Namespace and Class

In C#, every application resides in a namespace, and all the operational code goes inside a class. Here’s how you can set it up:

using System;

namespace MultiplicationTable
{
    class Program
    {
        static void Main(string[] args)
        {
            // Our code will go here
        }
    }
}

Step 2: Taking User Input

Inside the Main method, prompt the user to enter a number and read this input:

Console.Write("Enter a number to generate its multiplication table: ");
int number = Convert.ToInt32(Console.ReadLine());

Step 3: Generating the Multiplication Table

Use a for loop to iterate and generate the multiplication table:

for (int i = 1; i <= 10; i++)
{
    Console.WriteLine($"{number} x {i} = {number * i}");
}

Here is the complete program to print the multiplication table of a number in C#.

using System;

namespace MultiplicationTable
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number to generate its multiplication table: ");
            int number = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine($"{number} x {i} = {number * i}");
            }
        }
    }
}

After writing the program, you can run it in Visual Studio. The console window will prompt you to enter a number. Once you do, it will display the multiplication table for that number. Check out the screenshot for the output below:

Enter a number to generate its multiplication table: 5
5 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
write a c# program to print the multiplication table of a number

Conclusion

In this C# tutorial, I have explained how to write a C# program to print the multiplication table of a number.

You may also like: