33 C Program to Find Multiplication of two Binary Numbers
Posted by Superadmin on December 23 2015 03:09:54
This C Program Calculates Multiplication of two Binary Numbers. 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.
Here is source code of the C program to Calculate Multiplication of two Binary Numbers.The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Find Multiplication of two Binary Numbers
*/
#include <stdio.h>
int binaryproduct(int, int);
int main()
{
long binary1, binary2, multiply = 0;
int digit, factor = 1;
printf("Enter the first binary number: ");
scanf("%ld", &binary1);
printf("Enter the second binary number: ");
scanf("%ld", &binary2);
while (binary2 != 0)
{
digit = binary2 % 10;
if (digit == 1)
{
binary1 = binary1 * factor;
multiply = binaryproduct(binary1, multiply);
}
else
binary1 = binary1 * factor;
binary2 = binary2 / 10;
factor = 10;
}
printf("Product of two binary numbers: %ld", multiply);
return 0;
}
int binaryproduct(int binary1, int binary2)
{
int i = 0, remainder = 0, sum[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;
}
Output:
$ cc pgm10.c
$ a.out
Enter the first binary number: 10010
Enter the second binary number: 10101
Product of two binary numbers: 101111010