The Program will give an out put for Once complemet for a input Integer number :
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int GetIntegerComplement(int nInputNum)
{
int nRemider = 0, nCompRem = 0, nSum = 0, nCompSum=0, nTempNum;
int nPos=1;
string strComp="";
char ChArray[2];
size_t Size = 1;
nTempNum = nInputNum;
do
{
nRemider = nTempNum % 2;
if (nRemider == 0)
{
nCompRem = 1;
}
else
{
nCompRem = 0;
}
nSum = nSum + (nPos*nRemider);
nCompSum = nCompSum + (nPos*nCompRem);
_itoa(nCompRem,ChArray,10);
strComp = strComp + string(ChArray);
nTempNum = nTempNum / 2;
nPos = nPos * 10;
} while (nTempNum>0);
cout << "\n The Binary equivalent of " << nInputNum << " is " << nSum;
int nIntComp = 0;
nPos = 0;
nRemider = 0;
while (nCompSum > 0)
{
nRemider = nCompSum % 10;
nIntComp = nIntComp + pow(2,nPos)*nRemider;
nCompSum = nCompSum / 10;
nPos++;
}
reverse(strComp.begin(),strComp.end());
cout << "\n The complement Binary equivalent" << " is " << strComp;
return nIntComp;
}
void main()
{
int nNumber;
cout << "\n Enter a number to Convert to Binary";
cin >> nNumber;
cout << "\n The Integer complement of " << nNumber << " is " << GetIntegerComplement(nNumber);
_getch();
}
OutPut :
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int GetIntegerComplement(int nInputNum)
{
int nRemider = 0, nCompRem = 0, nSum = 0, nCompSum=0, nTempNum;
int nPos=1;
string strComp="";
char ChArray[2];
size_t Size = 1;
nTempNum = nInputNum;
do
{
nRemider = nTempNum % 2;
if (nRemider == 0)
{
nCompRem = 1;
}
else
{
nCompRem = 0;
}
nSum = nSum + (nPos*nRemider);
nCompSum = nCompSum + (nPos*nCompRem);
_itoa(nCompRem,ChArray,10);
strComp = strComp + string(ChArray);
nTempNum = nTempNum / 2;
nPos = nPos * 10;
} while (nTempNum>0);
cout << "\n The Binary equivalent of " << nInputNum << " is " << nSum;
int nIntComp = 0;
nPos = 0;
nRemider = 0;
while (nCompSum > 0)
{
nRemider = nCompSum % 10;
nIntComp = nIntComp + pow(2,nPos)*nRemider;
nCompSum = nCompSum / 10;
nPos++;
}
reverse(strComp.begin(),strComp.end());
cout << "\n The complement Binary equivalent" << " is " << strComp;
return nIntComp;
}
void main()
{
int nNumber;
cout << "\n Enter a number to Convert to Binary";
cin >> nNumber;
cout << "\n The Integer complement of " << nNumber << " is " << GetIntegerComplement(nNumber);
_getch();
}
OutPut :