C# Program to Display Multiplication Table of a Given Number

Multiplication table

C# programs to display the multiplication table of a number have been shown here. Both the iterative and recursive approaches have been covered below.






1. C# Program & output to display multiplication table of a number using iteration

Code has been copied
/**************************************
	    alphabetacoder.com
C# Program to display multiplication
table of a given number using iteration
***************************************/

using System;
namespace MultiplicationTable {
    class Program {
        static void Main(string[] args) {
            // delclare variables
            int i, n, r;

            // take input
            Console.Write("Enter a number: ");
            n = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the range: ");
            r = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("\nMultiplication table of " + n + ": ");
            // display table
            for (i = 1; i <= r; i++) {
                Console.WriteLine(n + " x " + i + " = " + n * i);
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter a number: 6

Enter the range: 5

Multiplication table of 6:

6 x 1 = 6

6 x 2 = 12

6 x 3 = 18

6 x 4 = 24

6 x 5 = 30





2. C# Program & output to display multiplication table of a number using recursion

Code has been copied
/**************************************
	    alphabetacoder.com
C# Program to display multiplication
table of a given number using recursion
***************************************/

using System;
namespace MultiplicationTable {
    class Program {
        // recursive function to display 
        // multiplication table
        static void multiplication_table(int n, int r) {
            // exit condition of recursive call
            if (r == 0)
                return;
            // call function
            multiplication_table(n, r - 1);
            // display the line
            Console.WriteLine(n + " x " + r + " = " + n * r);
        }

        static void Main(string[] args) {
            // delclare variables
            int n, r;

            // take input
            Console.Write("Enter a number: ");
            n = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the range: ");
            r = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("\nMultiplication table of " + n + ": ");
            // call function to display multiplication table
            multiplication_table(n, r);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter a number: 10

Enter the range: 10

Multiplication table of 10:

10 x 1 = 10

10 x 2 = 20

10 x 3 = 30

10 x 4 = 40

10 x 5 = 50

10 x 6 = 60

10 x 7 = 70

10 x 8 = 80

10 x 9 = 90

10 x 10 = 100