This program takes two decimal number from the user and a operator for addition or substraction.it displays the binary equivalent of both number and also the added or substracted binary number .

jephthah
commented:
this is trash, and should never have been bumped -1

```
//////////////////////////////////////////////////////////////////////
//// This program add and substract two binary numbers
Taking the Input as a decimal number ///////
//////////////////////////////////////////////////////////////////////////
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
///////////////////////////////////////////////////////////////////////
/////////// programmer : Harsh chandra //////////////
/////////////////////////////////////////////////////////////////////
void binaryadd(int,int*,int*,int); /* funtion to add to binay numbers */
int a[8],b[8],c[8]; /* as we want 8 bits , here extern data type is required */
void main()
{ clrscr();
int n1,n2,i=0,carry=0,*m,*n,k,x,y,d[8]={1,0,0,0,0,0,0,0};
char op;
gotoxy(1,6);printf("Enter 1st Numbers :");
scanf("%d",&n1);
gotoxy(1,7);printf("Enter 2nd Numbers :");
scanf("%d",&n2);
oper: /* label to return back if user gives wrong operator */
gotoxy(1,11);printf(" ");
gotoxy(1,8);printf("\nEnter the operator(+,-) :");
op=getch();
x=n1; /* for future refence in case of substraction */
y=n2;
if(op =='+' || op=='-')
{ while(n1!=0) /* converting 1st number to its binary equivalent */
{ a[i]=n1 % 2;
n1/=2;
i++;
}
/* printing 1st number */
printf("\nThe binary of %d is : ",x);
for(k=7;k>=0;k--)
printf("%d",a[k]);
printf("\n");
i=0;
while(n2!=0)
{ b[i]=n2 % 2; /* converting 2nd number to its binary equivalent */
n2/=2;
i++;
}
/* printing binary of 2nd number */
printf("\nThe binary of %d number is :",y);
for(k=7;k>=0;k--)
printf("%d",b[k]);
printf("\n");
}
switch(op)
{ case '+':
i=0;
m=a;
n=b;
carry=0;
binaryadd(i,m,n,carry); /*function called for binary add */
printf("\nThe addition is :");
for(i=7;i>=0;i--)
{ printf("%d",c[i]);
}
break;
case '-':
for(i=0;i<=7;i++)
{ if(b[i]==0)
b[i]=1; /* 1's complement */
else
b[i]=0;
}
printf("\nThe 1's complement of %d is : ",y);
for(i=7;i>=0;i--)
printf("%d",b[i]);
i=0;
m=b;
n=d;
carry=0;
binaryadd(i,m,n,carry); /* called for 2's complement */
printf("\nThe 2's complement of %d is : ",y);
for(i=7;i>=0;i--)
printf("%d",c[i]);
for(i=0;i<=7;i++)
b[i]=c[i];
i=0;
m=a;
n=b;
carry=0;
binaryadd(i,m,n,carry);
if(x>y)
{ printf("\nThe substaction is : ");
for(i=7;i>=0;i--)
printf("%d",c[i]);
}
if(x < y)
{ for(i=7;i>=0;i--)
{ if(c[i]==0)
c[i]=1;
else
c[i]=0;
}
i=0;
m=c;
n=d;
carry=0;
binaryadd(i,m,n,carry);/* again 2's complement for negative answers */
printf("\nThe subsraction is : ");
printf("-");
for(i=7;i>=0;i--)
printf("%d",c[i]);
}
exit(0);
break;
default:
gotoxy(1,11);printf("WRONG OPTION");
getch();
goto oper;
}
getch();
}
void binaryadd(int i,int *m,int *n,int carry)
{ if(*m==1 && *n==1 && carry==1)
{ c[i]=1;
carry=1;
}
if(*m==1 && *n==1 && carry==0)
{ c[i]=0;
carry=1;
}
if(*m==0 && *n==1 && carry==0)
{ c[i]=1;
carry=0;
}
if(*m==0 && *n==1 && carry==1)
{ c[i]=0;
carry=1;
}
if(*m==1 && *n==0 && carry==0)
{ c[i]=1;
carry=0;
}
if(*m==1 && *n==0 && carry==1)
{ c[i]=0;
carry=1;
}
if(*m==0 && *n==0 && carry==0)
{ c[i]=0;
carry=0;
}
if(*m==0 && *n==0 && carry==1)
{ c[i]=1;
carry=0;
}
i++;
m++;
n++;
if(i<=8) /* Base value of recursion */
binaryadd(i,m,n,carry); /* recursion till we reach 8 bit of number */
}
```

About the Author

Student Of B.Tech