C# Program to Print Multiplication Table of a Given Number

Introduction

In this article, we’ll explore how to create a simple C# Program to Print Multiplication Table of a Given Number. This program is helpful for both beginners and professionals.

It will ensure you a clear understanding of each step. We will provide multiple C# code examples with code comments and explanations.

Understanding the Problem

Before we dive into coding, let’s break down the problem into smaller steps:

  • Accept User Input: The program should prompt users to enter a number for which they want to generate the multiplication table.
  • Generate Multiplication Table: Once the user provides a number, the program must calculate and print the multiplication table for that specific number.
  • Displaying the Table: The program should display the multiplication table in a readable format, including the product of the given number multiplied by each integer from 1 to 10.

Now, let’s proceed with the C# code examples.

Code Example 1: C# Program to Print Multiplication Table of a Given Number

using System;

class MultiplicationTable
{
    static void Main()
    {
        // Step 1: Accept User Input
        Console.Write("Enter a number: ");
        int number = Convert.ToInt32(Console.ReadLine());

        // Step 2: Generate Multiplication Table
        Console.WriteLine($"The table is for {number}:");

        // Step 3: Displaying the Table
        for (int i = 1; i <= 10; i++)
        {
            int result = number * i;
            Console.WriteLine($"\t {number} x {i} = {result};");
        }
    }
}

Output:

Enter a number: 5
The table is for 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;

Explanation:

  • We use the Console.ReadLine method to get the user input.
  • The entered number is stored in the variable number.
  • A for loop iterates from 1 to 10, calculating and printing the multiplication results.

Code Example 2: Custom Range Multiplication Table

using System;

class CustomRangeMultiplicationTable
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());

        Console.Write("Enter the range: ");
        int range = int.Parse(Console.ReadLine());

        Console.WriteLine($"The table is for {number} up to {range}:");

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

Output:

Enter a number: 15
Enter the range: 10
The table is for 15 up to 10:
	 15 x 1 = 15;
	 15 x 2 = 30;
	 15 x 3 = 45;
	 15 x 4 = 60;
	 15 x 5 = 75;
	 15 x 6 = 90;
	 15 x 7 = 105;
	 15 x 8 = 120;
	 15 x 9 = 135;
	 15 x 10 = 150;

Conclusion

In this article, we’ve explored the basics of creating a C# program to print the multiplication table of a given number. Feel free to experiment with the code, modify it according to your needs, and continue your journey to mastering C#.

Recommended Articles:

Shekh Ali
4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments