Description:
#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
Conclusion: the program is error free
VIVA QUESTIONS:
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: In the given binary number first covert 0 to1 and 1 to 0. And finally add the 1 to the converted number. Then we will get the 2’s complement number.
Program
No comments:
Post a Comment