# C Program to Multiply Two Binary Numbers

«
»

This is a C program to Calculate Multiplication of two Binary Numbers.

Problem Description

This program takes two binary numbers as input and multiply them.

Problem Solution

1. Take two binary numbers as input.
2. Do the repeated addition of binary numbers.
3. The result is the output.

Program/Source Code

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.

1. `/*`
2. ` * C Program to Find Multiplication of two Binary Numbers`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int binaryproduct(int, int);`
7. ` `
8. `int main()`
9. `{`
10. ` `
11. `    long binary1, binary2, multiply = 0;`
12. `    int digit, factor = 1;`
13. ` `
14. `    printf("Enter the first binary number: ");`
15. `    scanf("%ld", &binary1);`
16. `    printf("Enter the second binary number: ");`
17. `    scanf("%ld", &binary2);`
18. `    while (binary2 != 0)`
19. `    {`
20. `        digit =  binary2 % 10;`
21. `        if (digit == 1)`
22. `        {`
23. `            binary1 = binary1 * factor;`
24. `            multiply = binaryproduct(binary1, multiply);`
25. `        }`
26. `        else`
27. `            binary1 = binary1 * factor;`
28. `        binary2 = binary2 / 10;`
29. `        factor = 10;`
30. `    }`
31. `    printf("Product of two binary numbers: %ld", multiply);`
32. `    return 0;`
33. `}`
34. ` `
35. `int binaryproduct(int binary1, int binary2)`
36. `{`
37. `    int i = 0, remainder = 0, sum;`
38. `    int binaryprod = 0;`
39. ` `
40. `    while (binary1 != 0 || binary2 != 0)`
41. `    {`
42. `        sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;`
43. `        remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;`
44. `        binary1 = binary1 / 10;`
45. `        binary2 = binary2 / 10;`
46. `    }`
47. `    if (remainder != 0)`
48. `        sum[i++] = remainder;`
49. `    --i;`
50. `    while (i >= 0)`
51. `        binaryprod = binaryprod * 10 + sum[i--];`
52. `    return binaryprod;`
53. `}`
Program Explanation

1. Take two binary numbers as input and store it in the variables binary1 and binary2. Initialize the variables multiply and factor with 0 and 1 respectively.
2. Divide the variable binary2 by 10 and obtain its remainder. Store this remainder in the variable digit.
3. Check if the digit is equal to 1 or 0. If it is 1, then multiply binary 1 with factor and override binary1 with this value. Call function binaryproduct() by passing binary1 and multiply as parameters.
4. If it is 0, then multiply binary 1 with factor and override binary1 with this value and override binary2 with its quotient got when it is divided by 10.
5. Do steps 2-4 until binary2 becomes zero.
6. In the function binaryproduct(), obtain the remainder and quotient of both the parameters.
7. Firstly add the remainders of both parameters and further add the variable remainder.
8. Obtain the remainder and quotient of the result got at step 7 when divided by 2. Store the remainder in the array sum[] and override the variable remainder with the quotient.
9. Override the variables binary1 and binary2 with their quotient got at step 6.
10. Repeat the steps 6-9 with the new values of binary1 and binary2 until both becomes zero.
11. When it becomes zero check if any remainder exits. If it is, then copy it into the array sum.
12. Multiply the variable binaryprod with 10 and add the result to array sum. Override the variable binaryprod with the got result. Do this step for all array elements and return binaryprod.
13. Print the output and exit.

Runtime Test Cases
```Output:

Enter the first binary number: 10010
Enter the second binary number: 10101
Product of two binary numbers: 101111010```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples. 