Program to find the 2's complement of a binary number.

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[10],b[10],c;
int i,count=0;
cout<<"Enter the binary number : ";
gets(a);
strcpy(b,a);
for(i=0;i<10;i++)
{
if(a[i]=='1')
{
a[i]='0';
count++;
}
else if(a[i]=='0')
{
a[i]='1';
count++;
}
}
i=count-1;
c='1';
do
{
if(a[i]=='1')
{
a[i]='0';
c='1';
}
else if(a[i]=='0')
{
a[i]='1';
c='0';
}
i--;
}while(c=='1');
cout<<"The 2's Compliment of "<<b;
cout<<" is:"<<a<<endl;
cout<<"/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
return 0;
}

Output:



Comments

Popular posts from this blog

Program to illustrate the concept of templates.

To create an html file to implement the concept of margin, padding using cascading style sheets.

To configure the IP address for a computer connected to LAN