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.
-
using System;
- Imports the
System
namespace, which allows the use of built-in functions likeConsole.WriteLine()
.
- Imports the
-
namespace multiply_two_numbers
- Defines a namespace named
multiply_two_numbers
to group related classes.
- Defines a namespace named
-
class MultiplyTwoNumbers
- Declares a class named
MultiplyTwoNumbers
, which contains the program logic.
- Declares a class named
-
static void Main(String[] args)
- The
Main
method is the entry point of the program.
- The
-
Declaring Variables:
int a, b;
→ Declares two integer variables to store user input.
-
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
.
- Reads the user input, converts it from string to integer, and stores it in
-
Similarly, the second number is read and stored in
b
.
-
-
Multiplication Operation:
int mul = a * b;
- Computes the product of
a
andb
and stores it inmul
.
- Computes the product of
-
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