Binary Add and Substraction

harshchandra -2 Tallied Votes 610 Views Share

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 */
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Code doesn't seem to be very portable!
What is your compiler and operating system?

zayalaksme 0 Newbie Poster

sir please write a c program for addition and subtraction for "SIGNED BINARY NUMBERS"

simnix -1 Newbie Poster

This code looks like it is meant for an embedded processor, unless they have the correct compiler for the processor and the correct programmer this code is as much use as toilet paper.

jephthah commented: incorrect comment on a worthless snippet -1
jephthah 1,888 Posting Maven

it's not embedded, it's TurboC.

yes, it is toilet paper. and now, after two years, you just floated it back to the top of the bowl. Thanks :-/

xavier666 56 Junior Poster

I was expecting someone to explain him the problems about his code. Ah well ...

Hi harshchandra,
Thank you for presenting your code snippet but your code has some major faults, hence lost it's appeal. Your program displays the bad practices of C. They are as follows

  • conio.h - It is not a standard C header file and is only used in old MS-DOS compilers. You can see about it here
  • void main() - Let's just say 'void main'ers are doomed' (-Salem)
  • goto - There are a lot of alternatives to goto . It is against structured programming
  • getch() & clrscr() come under conio.h, so no need to repeat

This is an excellent link about the bad practices of C (explaining WHY they are bad) and what to do to avoid them.

anonymous alias commented: pay attention, dude. this crap is over 5 years old. it doenst need obvious commentary. +0
simnix -1 Newbie Poster

Then can you explain why you need the piece of code "gotoxy(1,6)" if it is not intended for embedded processors. This piece code is usually used to place the character on an say a 2x16 LCD.

uskok 0 Newbie Poster

It's very likely that chandra will take your advice and answer your questions when he comes back.
The code was posted in 2004 and his last activity was in february of 2007, but I'm sure it will be soon. :confused:

Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster

>> can explain why you need the piece of code "gotoxy(1,6)"

It's an ancient function from Turbo C++ and is used to put the cursor on a (x,y) position on the screen.
Now please stop reviving these old threads. You can safely assume that if a thread is older then 2 weeks, it's dead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.