C Program to Find Ones Complement of a Binary Number

  • Write a C program to read a binary number and print its ones complement in c.
  • Wap in C to find ones complement of a binary number.
  • Binary One’s Complement.
  • One’s And Two’s Complement.

Recommended Reading On: C Program to Find Twos Complement of a Binary Number

Required Knowledge

Algorithm to find ones complement of a binary number

  • To find the ones complement of a number, we will toggle the bits of the number. Change all 1’s to 0’s and all 0’s to 1’s.

For Example :

Binary Number = 00101011
Ones Complement = 11010100

C program to find ones complement of a number

C program to find ones complement of a number

#include <stdio.h>  
#include <string.h>  
   
int main() {
    char binaryNumber[100], onesComplement[100];  
    int counter, error=0, digitCount;  
   
    /*
     * Take a binary string as input from user
     */
    printf("Enter a Binary Number\n");  
    scanf("%s", binaryNumber);  
   
    /* 
     * To get one's complement, we toggle 
     * 1's to 0's and 0's to 1's
     */
    digitCount = strlen(binaryNumber);
     
 for(counter=0; counter < digitCount; counter++) {  
        if(binaryNumber[counter]=='1') {  
            onesComplement[counter] = '0';  
        } else if(binaryNumber[counter]=='0') {  
            onesComplement[counter] = '1';  
        } else {  
            printf("Error :( ");  
            return 1;
        }  
    }  
    onesComplement[digitCount] = '\0';
       
    printf("Ones Complement : %s", onesComplement);  
   
    return 0;  
}

Output

Enter a Binary Number
11110010101
Ones Complement : 00001101010
Enter a Binary Number
10001111
Ones Complement : 01110000

Check Yourself after reading the above information for more perfection:

  • Write A Program In C To Read A Number And Find One’S Complement?
  • C Program To Find Ones Complement Of A Binary Number?
  • How To Find One’s Complement In C?
  • Write A C Program To Find One’s Complement Of A Binary Number?
  • C Program To Find 1’S Complement Of A Number?
  • C Program To Find Complement Of A Number?
  • C Program To Find Complement Of A Set?
  • How To Find 1s Complement?
  • Find 1’S Complement?
  • Find One’s Complement?
  • How To Find Two’s Complement?