MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

C# program to print multiplication table

This program can be used to print any multiplication table until any number


using System;


namespace SamplePrograms
{
    class NumberTable
    {
        public static void Main()
        {
            // Prompt the user to enter number for multiplication table
            Console.WriteLine("For which number do you want to print multiplication table");


            // Read the number from console and convert to integer
            int Number = Convert.ToInt32(Console.ReadLine());


            // Prompt the user for multiplication table target
            Console.WriteLine("What is your target? 10, 20, 30 etc...");


            // Read the target from console and convert to integer
            int Target = Convert.ToInt32(Console.ReadLine());


            // Loop to print multiplication table until we reach the target
            for (int i = 1; i <= Target; i++)
            {
                // Compute multiplication result
                int Result = Number * i;


                // Format and Print the multiplication table
                Console.WriteLine(Number.ToString() + " X " + i.ToString() + 
                                  " = " + Result.ToString());


                // The above line can also be rewritten as shown below.
                // Console.WriteLine("{0} X {1} = {2}", Number, i, Result);
            }
        }
    }
}

1 comment:

  1. Console.WriteLine("For Which number you want the multiplication table?");
    int Num = 0;
    Num = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("Till where yiu want to do the multiplication?");
    int mul = 1;
    mul = Convert.ToInt32(Console.ReadLine());
    int total = 0;
    for (int i = 1; i <= mul; i++) {
    total = i * Num;
    Console.WriteLine(Num + "*" + i + " = " + total);
    }

    Console.ReadKey();

    ReplyDelete