In this article, we are going to learn how to find the One’s Complement of a Binary number entered by the user. A binary number consists of 0s and 1s in its composition. We can get the One’s Complement of a Binary number by just inverting the 0s to 1s and 1s to 0s. It is just swapping the bits.

We are going to take input from the user as a binary number and then we will iterate over this number one bit at a time. With each iteration, we will check if the bit is 1 or 0. Then by using the if-else statement we will invert the 1 to 0 and 0 to 1. This will give us the 1s complement when we will reach the end of iterations.

### C++ Program to Find 1s Complement of Binary number

In this example, we are trying to find one’s complement of an 8-bit binary number. This program will work for 8-bit binary numbers, if you want to use a smaller or bigger number then please adjust the array size.

```
#include <iostream>
using namespace std;
int main()
{
char binNum[8 + 1], binComp[8 + 1];
cout <<"Please enter 8 bit binary number : ";
cin >> binNum;
for(int i = 0; i < 8; i++)
{
if(binNum[i] == '1')
{
binComp[i] = '0';
}
else if(binNum[i] == '0')
{
binComp[i] = '1';
}
else
{
cout<<"You entered a Non-Binary Number.Exiting Program";
exit(0);
}
}
binComp[8] = '
```#include <iostream>
using namespace std;
int main()
{
char binNum[8 + 1], binComp[8 + 1];
cout <<"Please enter 8 bit binary number : ";
cin >> binNum;
for(int i = 0; i < 8; i++)
{
if(binNum[i] == '1')
{
binComp[i] = '0';
}
else if(binNum[i] == '0')
{
binComp[i] = '1';
}
else
{
cout<<"You entered a Non-Binary Number.Exiting Program";
exit(0);
}
}
binComp[8] = '\0';
cout<<"Ones complement of " << binNum << " is " << binComp ;
return 0;
}

';
cout<<"Ones complement of " << binNum << " is " << binComp ;
return 0;
}

Output

```
Please enter 8 bit binary number : 11111111
Ones complement of 11111111 is 00000000
```