To Write C Program for Multiplication of Two Binary Numbers

Aim:

To write C program for multiplication of two binary numbers.

Algorithm:

Step 1: Convert the input into binaries and store it in variable and array
Step 2: Add the variable binaries with array[i] , i++;
Step 3: Add every result by multiply () function
Step 4: Display the last result
Step 5: End

Program:

#include<stdio.h>
#include <stdlib.h>
int long add(int long k,int long h)
{	
int sum[100];
int i=0,temp=0;
printf("\n");
while(k!=0||h!=0)
{
sum[i]=(k%10+h%10+temp)%2;
temp=(k%10+h%10+temp)/2;
k=k/10;
h=h/10;	
i++;
}
if(temp!=0)
{
sum[i]=temp;	
}
int temp1=1,i1;
int long sum3=0;
i1=i;
for(i=0;i<i1;i++)
{
if(sum[i]==0)
{
temp1=temp1*10;
}
else{
sum3=sum3+temp1;
temp1=temp1*10;	
}
}	
return sum3;
int x,sum=0,y,a[100],b[100],j=1;
 int i=0,temp=0;
 int long long sum1=0;
printf("\nENTER THE 2 NO'S :\n");
scanf("%d%d",&x,&y);
while(x>0)
{
a[i]=x%2;
x=x/2;
if(a[i]==0)
{
j=j*10;
}
else{
sum=sum+j;
j=j*10;	
}
i++;
}
j=1;
int s=0;
int sum4=0;
while(y>0)
{
b[s]=y%2;
y=y/2;
if(b[s]==0)
{
j=j*10;
}
else{
sum4=sum4+j;
j=j*10;	
}
s++;	
}
printf("BINARY OF 1ST NUM : %d\n",sum);
printf("BINARY OF 2nd NUM : %d\n",sum4);
j=s;
i=1;
sum1=b[0]*sum;
s=1;
int long sum2;
while(s<j)
{
i=i*10;
sum2=b[s]*sum*i;
add(sum1,sum2);
sum1=add(sum1,sum2);
s++;
}
printf("MULTIPLY OF 2 NO'S :");
printf("%ld",sum1);
return 0;
}

Execution:

Input:
90 67

Output:
ENTER THE 2 NO'S :
BINARY OF 1ST NUM : 101101
BINARY OF 2nd NUM : 111111

Result:

Thus the Program for Binary Multiplication was executed Successfully.

Leave a Comment