C program for multiplication of two binary numbers



C code for product of two binary numbers

C program for multiplication of two binary numbers



#include
<stdio.h>

int binaryAddition(int,int);

int main(){

    long int binary1,binary2,multiply=0;
    int digit,factor=1;

    printf("Enter any first binary number: ");
    scanf("%ld",&binary1);
    printf("Enter any second binary number: ");
    scanf("%ld",&binary2);

    while(binary2!=0){
         digit =  binary2 %10;

         if(digit ==1){
                 binary1=binary1*factor;
                 multiply = binaryAddition(binary1,multiply);
         }
         else
             binary1=binary1*factor;
   
         binary2 = binary2/10;
         factor = 10;
    }

    printf("Product of two binary numbers: %ld",multiply);
   
   return 0;
}

int binaryAddition(int binary1,int binary2){

    int i=0,remainder = 0,sum[20];
    int binarySum=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)
         binarySum = binarySum*10 + sum[i--];

    return binarySum;
}

Sample output:

Enter any first binary number: 1101
Enter any second binary number: 11
Product of two binary numbers: 100111


Algorithm:

Rule of binary multiplication:

0 * 0 = 0
1 * 0 = 0
0 * 1 = 0
1 * 1 = 1

Q. what is the product of the binary numbers 0110 and 0011?

Answer: 0110 * 0011 = 10010




6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.
Post a Comment