Write a C program to input binary number from user and find ones complement of binary number using loop

Write a C program to input binary number from user and find ones complement of binary number using loop. How to find 1s complement of a binary number in C programming. Logic to find ones complement of binary number in C programming.

/** * C program to find 1s complement of a binary number */ #include <stdio.h> #define SIZE 8 int main() { char binary[SIZE + 1], onesComp[SIZE + 1]; int i, error=0; printf("Enter %d bit binary value: ", SIZE); /* Input binary string from user */ gets(binary); /* Store all inverted bits of binary value to onesComp */ for(i=0; i<SIZE; i++) { if(binary[i] == '1') { onesComp[i] = '0'; } else if(binary[i] == '0') { onesComp[i] = '1'; } else { printf("Invalid Input"); error = 1; /* Exits from loop */ break; } } /* Marks the end of onesComp string */ onesComp[SIZE] = '\0'; /* Check if there are binary string contains no error */ if(error == 0) { printf("Original binary = %s\n", binary); printf("Ones complement = %s", onesComp); } return 0; }

output:

Enter any 8 bit binary value: 00001111
Original binary = 00001111
Ones complement = 11110000

No comments:

Post a Comment