C# program to print table of any given number by using recursion
Here's a C# program that prints the table of a given number using recursion:
using System;
class Program
{
static void Main()
{
int number = 5;
int count = 1;
Console.WriteLine($"Table of {number}:");
PrintTable(number, count);
}
static void PrintTable(int number, int count)
{
if (count > 10)
{
return;
}
Console.WriteLine($"{number} x {count} = {number * count}");
PrintTable(number, count + 1);
}
}
In this program, we have the PrintTable method that prints the table of a given number. It takes the number to generate the table for (number) and the current count of the multiplication (count) as parameters.
The base case of the recursion is when the count exceeds 10. In this case, the method simply returns and terminates the recursion.
In the recursive case, it first prints the multiplication of the number with the current count. Then, it makes a recursive call to PrintTable with the same number and count + 1 to print the next multiplication in the table.
The Main method initializes the number to generate the table for (5) and the initial count (1). It calls the PrintTable method to print the table of the given number. The program prints the table by multiplying the number with each count from 1 to 10.
In the example above, the output would be:
Table of 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
These are the multiplications of 5 with each count from 1 to 10, representing the table of 5.