Home > other >  Input any mathematical table without using loops
Input any mathematical table without using loops

Time:05-25

I need to print mathematical table without using any loop (for, while, do while, etc.). Can anyone help me out, the easiest example I could find was writing console.writeline 10times for each line.

This is my code!

using System;
using System.Linq;
class Question4
{

 int product, i=1;
  public static void Main(string[] args)
 {
  int num;
  Console.Write("Enter the Number to Print its Multiplication Table: ");
  num = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine("\nMultiplication Table For {0}: ",num);
  TableFunctionName (num);
   }
   public void TableFunctionName(int n)
  {
   if(i<=10)
  {
   table=n*i;
    Console.WriteLine("{0} x {1} = {2}",n,i,table);
    i  ;
     }
   return;

     }

     }

CodePudding user response:

Using recursion

  static   void Multiply(int a, int b) {
        if (a > 1)
            Multiply(a - 1, b);
        Console.WriteLine($"{a} * { b} = {a * b}");
    }
    static void Main(string[] args) {

        Multiply(10, 5);
    }
}

CodePudding user response:

You could use recursion

public static void Main()
{
    Console.Write("Enter the Number to Print its Multiplication Table: ");
    var input = Console.ReadLine();
    var number = Convert.ToInt32(input);
    
    CalculateAndPrint(number, 1);
}

static void CalculateAndPrint(int number, int factor)
{
    if (factor > 9) return;
    
    Console.WriteLine("{0} x {1} = {2}", number, factor, number * factor);
    CalculateAndPrint(number,   factor);
}
  • Related