C# Program to multiply two numbers

Program

using System;
namespace multiply_two_numbers
{
	class MultiplyTwoNumbers
	{
		static void Main(String[] args)
		{
			int a, b;
			Console.Write("Enter first number:\t");
			a = int.Parse(Console.ReadLine());
			Console.Write("Enter second number:\t");
			b = int.Parse(Console.ReadLine());
			int mul = a * b;
			Console.WriteLine("Product of two numbers: " + mul);
		}
	}
}

This C# program multiplies two numbers entered by the user and displays the result.

  1. using System;

    • Imports the System namespace, which allows the use of built-in functions like Console.WriteLine().
  2. namespace multiply_two_numbers

    • Defines a namespace named multiply_two_numbers to group related classes.
  3. class MultiplyTwoNumbers

    • Declares a class named MultiplyTwoNumbers, which contains the program logic.
  4. static void Main(String[] args)

    • The Main method is the entry point of the program.
  5. Declaring Variables:

    • int a, b; → Declares two integer variables to store user input.
  6. Reading User Input:

    • Console.Write("Enter first number:\t");

      • Displays a prompt to enter the first number.
    • a = int.Parse(Console.ReadLine());

      • Reads the user input, converts it from string to integer, and stores it in a.
    • Similarly, the second number is read and stored in b.

  7. Multiplication Operation:

    • int mul = a * b;
      • Computes the product of a and b and stores it in mul.
  8. Displaying the Result:

    • Console.WriteLine("Product of two numbers: " + mul);
      • Prints the multiplication result to the console.

Output

$ mcs MultiplyTwoNumbers.cs 
$ mono MultiplyTwoNumbers.exe 
Enter first number:	12
Enter second number:	9
Product of two numbers: 108