C++ Program to Find 1’s Complement of a Binary Number
#include<iostream.h> #include<conio.h> using namespace std; class test { string sk; public: void read(void) { cout<<"Enter the Binary Number:"; cin>>sk; } void bin_num(void) { for(int i=0;i<sk.length();i++) { if(sk.at(i)!='0' && sk.at(i)!='1') { cout<<"Incorrect binary number!" getch(); exit(0); } } } void binary(void) { bin_num(); for(i=0;i<sk.length();i++) { if(sk.at(i)=='0') sk.at(i)='1'; else sk.at[i]='0'; } } void display(void) { binary(); cout<<"The 1's Complement of this Binary Number is :"<<sk; } }; int main() { test t1; t1.read(); t1.display(); getch(); return 0; }
Output:
Enter the Binary Number: 101101 The 1's Complement of this Binary Number is : 010010