Multiply Two Numbers without using * (Multiplication Operator) in C#

Hi Programmers, welcome to new article of c#.net. this article i’ll write the program to Multiply Two Numbers without using Multiplication Operator in C# console application. To Achieve the output we should use Logical XOR operator , Shift operator and Conditional AND operator. Let’s see the codes.

using bitwise operator

Directly Test Below codes into Editor.

using System;
namespace ConsoleApp1 
{
    class Program
    {
        static int add(int a,int b)
        {
            int carry;
            while(b != 0)
            {
                carry = (a & b) << 1;
                a = a ^ b;
                b = carry;
            }
            return a;
        }

        static void Main(string[] args)
        {
            int n1, n2, mul = 0, i;
            Console.WriteLine("Enter First Number : ");
            n1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter Second Number : ");
            n2 = int.Parse(Console.ReadLine());

            for(i = 0; i < n2; i++)
            {
                mul = add(mul, n1);
            }
            Console.WriteLine("Multiplication of " + n1 + " and " + n2 + " is " + mul);
        }       
    }
}

Happy Coding…Thanks.

Post Author: adama