C sharp program to multiply two numbers

Example of C# program to multiply two numbers with and without user defined function. Get input from user in C# program below as number 1 and number 2, multiply two numbers and print the result.

As an example, two integer values from the user input will be taken using Console class in c# in the program and will be multiplied using the simple expression as multiplication = first *second.

Following is the output for the below programs.

Output
Please enter Number 1
4
Please enter Number 2
5
multiplication of 4 and 5 is 20

C# source code example

Method-1 using * operator

Example Program 1- multiplication of 2 numbers using * operator.

Code

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter Number 1");

            int num1;
            bool flag = false;
            flag = int.TryParse(Console.ReadLine(), out num1);

            if (flag == false)
            {
                Console.WriteLine("Number 1  is a invalid number");
                return;

            }
            flag = false;
      

            int num2;
            Console.WriteLine("Please enter Number 2");
            flag = int.TryParse(Console.ReadLine(), out num2);

            if (flag == false)
            {
                Console.WriteLine("Number 2  is a invalid number");
                return;
            }
          

            int result = num1 * num2;

            Console.WriteLine("multiplication of {0} and {1}  is  {2}", num1, num2, result);
        }

    }

Method-2 using <<,>>,& operators

Example Program 2- multiplication of 2 numbers using bitwise operators.

C# code

  class Multiplication
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter Number 1");

            int num1;
            bool flag=false;
            flag= int.TryParse(Console.ReadLine(), out num1);

            if(flag==false)
            {
                Console.WriteLine("Number 1  is a invalid number");
                return;
               
            }
             flag=false;
             int a = num1;

             int num2;
             Console.WriteLine("Please enter Number 2");
             flag = int.TryParse(Console.ReadLine(),out num2);
            
            if(flag==false)
            {
                Console.WriteLine("Number 2  is a invalid number");
                return;
            }
            int b = num2;
            int result = 0;

            while(num2 !=0)
            {
                if ((num2 & 1) == 1)
                    result = result + num1;
                num1 = num1 << 1; num2 = num2 >> 1;
            }


            Console.WriteLine("multiplication of {0} and {1}  is  {2}",a,b,result);
        }
    }

Method -3 using recursion

Example Program 3- multiplication of 2 numbers using recursion.

C# code

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter Number 1");

            int num1;
            bool flag = false;
            flag = int.TryParse(Console.ReadLine(), out num1);

            if (flag == false)
            {
                Console.WriteLine("Number 1  is a invalid number");
                return;

            }
            flag = false;
            int a = num1;

            int num2;
            Console.WriteLine("Please enter Number 2");
            flag = int.TryParse(Console.ReadLine(), out num2);

            if (flag == false)
            {
                Console.WriteLine("Number 2  is a invalid number");
                return;
            }
            int b = num2;
           
            if(num1<num2)
            {
                a = num2;
                b = num1;
            }
            Program p = new Program();
            Console.WriteLine("multiplication of {0} and {1}  is  {2}", a, b, p.Multiply(a, b));
        }

        public int Multiply(int num1,int num2)
        {
            if (num2 == 0)
                return 0;

            return num1 + Multiply(num1, num2 - 1);
        }
    }

Output
Please enter Number 1
10
Please enter Number 2
5
multiplication of 10 and 5 is 50

Method -4 using + and – operator with loop

Example Program 4 – multiplication of 2 numbers is using + and – operator with loop.

C# code

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter Number 1");

            int num1;
            bool flag = false;
            flag = int.TryParse(Console.ReadLine(), out num1);

            if (flag == false)
            {
                Console.WriteLine("Number 1  is a invalid number");
                return;

            }
            flag = false;
            int a = num1;

            int num2;
            Console.WriteLine("Please enter Number 2");
            flag = int.TryParse(Console.ReadLine(), out num2);

            if (flag == false)
            {
                Console.WriteLine("Number 2  is a invalid number");
                return;
            }
            int b = num2;
           
            if(num1<num2)
            {
                a = num2;
                b = num1;
            }
          
            

            int result=0;

            while(b !=0)
            {
                result=result+a;
                b--;
              
            }

            Console.WriteLine("multiplication of {0} and {1}  is  {2}", num1, num2, result);
        }

    }

Related Posts