C# program to multiply two numbers in different ways

C# program to multiply two numbers:

This post will show you how to multiply two numbers in C#. We will learn how to use constant numbers and how to take the numbers as inputs from the user. The program will take the numbers as inputs from the user, multiply these to find the result and print the result.

Multiplication operator in C#:

The * is known as the multiplication operator. This operator is used to find the multiplication of two numbers in C#. It has the following syntax:

FirstNumber * SecondNumber

It will find the multiplication of the numbers FirstNumber and SecondNumber. It returns the multiplication results of these numbers.

Let’s try it with examples.

Example 1: Multiplication of two pre-defined numbers:

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            int First = 22;
            int Second = 33;
            int Result = First * Second;

            Console.WriteLine(First + "*" + Second + "=" + Result);
        }
    }
}

Here,

  • First and Second are two given numbers.
  • It uses the multiplication operator to find the multiplication of these numbers. It assigns this value to the integer variable Result.
  • The last line is printing these two numbers and the multiplication result.

If you run this program, it will print the below output:

22 * 33 = 726

Example 2: Multiplication of two user-given numbers:

Let’s take these numbers as inputs from the user and find out the multiplication result of two user-given numbers. Let’s take a look at the program:

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            int First, Second;

            Console.WriteLine("Enter the first number: ");
            First = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number: ");
            Second = Convert.ToInt32(Console.ReadLine());
            
            int Result = First * Second;

            Console.WriteLine(First + "*" + Second + "=" + Result);
        }
    }
}

In this program,

  • It uses Console.WriteLine to ask the user to enter the numbers.
  • Console.ReadLine is used to read the user-input. It returns a string value. So, it uses Convert.ToInt32 to convert it to an integer.
  • The other lines are similar to the previous example.

If you run this program, it will ask the user to enter the numbers and it will calculate the result with these two numbers.

Enter the first number: 
12
Enter the second number: 
44
12 * 44 = 528

You might also like: