C# Program to Find Multiplication of two Binary Numbers

«
»

This is a C# Program to find multiplication of two binary numbers.

Problem Description

This C# Program Finds Multiplication of two Binary Numbers.

Problem Solution

Here Binary number is a number that can be represented using only two numeric symbols – 0 and 1. A number in base 2. This program multiplies the 2 binary numbers.

advertisement
Program/Source Code

Here is source code of the C# Program to Find Multiplication of two Binary Numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find Multiplication of two Binary Numbers
 */
using System;
class program
{
    public static void Main()
    {
 
        int binary1, binary2, multiply = 0;
        int digit, factor = 1;
        prog pg = new prog();
        Console.WriteLine("Enter the first binary number: ");
        binary1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second binary number: ");
        binary2 = int.Parse(Console.ReadLine());
        while (binary2 != 0)
        {
            digit =  binary2 % 10;
            if (digit == 1)
            {
                binary1 = binary1 * factor;
                multiply = pg.binaryproduct(binary1, multiply);
            }
            else {
                binary1 = binary1 * factor;
                binary2 = binary2 / 10;
                factor = 10;
            }
            Console.WriteLine("Product of two binary numbers: {0}", multiply);
            Console.ReadLine();
        }
    }
class prog
{
    public int binaryproduct(int binary1, int binary2)
    {
        int i = 0, remainder = 0;
        int[] sum = new int[20];
        int binaryprod = 0; 
        while (binary1 != 0 || binary2 != 0)
        {
            sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
            remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
            binary1 = binary1 / 10;
            binary2 = binary2 / 10;
        }
        if (remainder != 0)
            sum[i++] = remainder;
            --i;
        while (i >= 0)
           binaryprod = binaryprod * 10 + sum[i--];
        return binaryprod;
    }
}
Program Explanation

This C# program we are reading the first and second binary numbers using ‘binary1’ and ‘binary2’ variables respectively. Using while loop checks the value of ‘binary2’ variable is not equal to 0. If the condition is true, then execute the iteration of the loop.

advertisement

Compute the modulus of the value of ‘binary2’ variable by 10. If else condition statement is used to check the value of ‘digit’ variable is equal to 1. If the condition is true, then execute the statement. Multiply the value of ‘binary1’ variable with the value of ‘factor’ variable.

The binaryproduct() procedure is used to compute the product of binary number. Here Binary number is a number that can be represented using only two numeric symbols – 0 and 1, a number in base 2. Multiply the 2 binary numbers. If condition statement is used to check the value of ‘remainder’ variable is not equal to 0. If the condition is true, then execute the statement.

While loop is used to check the value of ‘i’ variable is greater than or equal to 0. If the condition is true, then execute the iteration of the loop. Multiply the value of ‘binaryprod’ variable with 10 and add the resulted value with the value of ‘sum[]’ array variable.

advertisement
Runtime Test Cases
 
Enter the first binary number : 1010                                  
Enter the second binary number : 1011                                  
Product of two binary numbers : 1101110

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn