__Description:__
In this program the given binary
number is first covert the numbers 0 to1 and 1 to 0. And finally add the 1 to
the converted number. Then we will get
the 2’s complement number.

**main program**

__Algorithm:__
Step 1: Start

Step 2: declare the subprogram
“complement(char *a)”

Step 3: initialize the variable i

Step 4: read the binary number

Step 5: perform the loop operation.
if it is true then follows. if not goto step 7

i)
for(i=0;a[i]!=’\0’;i++)

ii)
if(a[i]!=’0’&&a[i]!=’1’) then displayed the
number is not valid. enter the correct number.

iii)
Exit the loop

Step 6: call sub program
‘complemt(a)’

Step 7: stop

__Sub program:__
Step 1: initialize
the variable I,c=0,b[160

Step 2:
1=strlen(a)

Step 3: perform
the loop operation. if it is true then follows. if not goto

i)for(i=l-1;i>=0;i--)

ii)if(a[i]==’0’) then b[i]=’1’
else

iii)b[i]=’0’

Step 4:
for(i=l-1;i>=0;i--) is true

i)
if(i==l-1) then

ii)
if(b[i]==’0’) then b[i]=’1’ else

iii)
b[i]=’0’,c=1 if not goto step 5

Step 5:
if(c==1&&b[i]==’0’) is true then

i)
b[i]=’1’, c=0 if not goto Step 6

Step 6:
if(c==1&&b[i]==’1’) then b[i]=’0’,c=1

Step 7: displayed
b[l]=’\0’

Step 8: print b
and return to main program

__Program:__
#include <stdio.h>

#include<conio.h>

void complement (char *a);

void main()

{

char a[16];

int i;

clrscr();

printf("Enter the binary number");

gets(a);

for(i=0;a[i]!='\0'; i++)

{

if (a[i]!='0' && a[i]!='1')

{

printf("The number entered
is not a binary number. Enter the correct number");

exit(0);

}

}

complement(a);

getch();

}

void complement (char *a)

{

int l, i, c=0;

char b[16];

l=strlen(a);

for (i=l-1; i>=0; i--)

{

if (a[i]=='0')

b[i]='1';

else

b[i]='0';

}

for(i=l-1; i>=0; i--)

{

if(i==l-1)

{

if (b[i]=='0')

b[i]='1';

else

{

b[i]='0';

c=1;

}

}

else

{

if(c==1 &&
b[i]=='0')

{

b[i]='1';

c=0;

}

else if (c==1 && b[i]=='1')

{

b[i]='0';

c=1;

}

}

}

b[l]='\0';

printf("The 2's complement is
%s", b);

}

__Output:__
1.Enter the binary number101010

The 2's complement is 010110

Enter the binary number11111

The 2's complement is 00001

Enter the binary number2222

The number entered is not a binary number. Enter the correct
number

**the program is error free**

__Conclusion:__

__VIVA QUESATIONS:__
1) Expand ASCII ?

Ans: American standarad code for information interchange

2)What is binary number ?

Ans: The number which contains only 0 and 1 is called binary
number.

3) Define 2”s complement ?

Ans: The given binary number is first covert the numbers 0
to1 and 1 to 0. And finally add the 1 to the converted number. Then we will get the 2’s complement number.

The program which you have given has a bug. There is an extra curly bracket.

ReplyDeleteAlso, the main's bracket is put at the wrong place. It will not compile. Please make the correct changes.

Delete