This is a C Program to perform binary addition of strings and print it.
This C Program Performs Binary Addition of strings and Print it.
Take input from the user and performs binary addition as shown in the program below.
Here is source code of the C Program to Perform Binary Addition of strings and Print it. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Perform Binary Addition of Strings and Print it */ #include <stdio.h> #include <string.h> /* global variables */ char s1[10], s2[10], s3[10]; int i, k; char carry = '0'; /* function prototype */ void binary_add(char *,char *); void main() { printf("enter string1\n"); scanf(" %[^\n]s", s1); printf("enter string2\n"); scanf(" %[^\n]s", s2); binary_add(s1, s2); printf("binary addition of number is\n"); if (carry == '1') { s3[i] = '1'; for (i = 1;i <= k + 1;i++) printf("%c", s3[i]); printf("\n"); } else { for (i = 1;i <= k + 1;i++) printf("%c", s3[i]); printf("\n"); } } /* function to add two binary numbers in a string */ void binary_add(char *s1, char *s2) { char *p1, *p2; p1 = s1; p2 = s2; k = strlen(s1); for (;*p1 != '\0' && *p2 != '\0';p1++, p2++); p1--; p2--; s3[k+1] = '\0'; for (i = k + 1;i >= 1;i--, p1--, p2--) { if (*p1 == '0' && *p2 == '0'&& carry == '0') { s3[i] = (*p1 ^ *p2) ^ carry; carry = '0'; } else if (*p1 == '0' && *p2 == '0' && carry == '1') { s3[i] = (*p1 ^ *p2)^ carry; carry = '0'; } else if (*p1 == '0' && *p2 == '1' && carry == '0') { s3[i] = (*p1 ^ *p2)^ carry; carry = '0'; } else if (*p1 == '0' && *p2 == '1' && carry == '1') { s3[i] = (*p1 ^ *p2)^ carry; carry = '1'; } else if (*p1 == '1' && *p2 == '0' && carry == '0') { s3[i] = (*p1 ^ *p2)^ carry; carry = '0'; } else if (*p1 == '1' && *p2 == '0' && carry == '1') { s3[i] = (*p1 ^ *p2)^ carry; carry = '1'; } else if (*p1 == '1' && *p2 == '1' && carry == '0') { s3[i] = (*p1 ^ *p2)^ carry; carry = '1'; } else { s3[i] = (*p1 ^ *p2)^ carry; carry = '1'; } } }
In this C Program, we are reading the string1 and string2 using ‘s1’ and ‘s2’ variables respectively. The binary_add() function is used to add two binary numbers in a string.
Nested if else condition statement is used to check that three variable values string1, string2 and carry variable values should be {000,001,010,011,100,101,110} using logical AND operator.
If any one of the condition is true then execute the statement. Using Binary OR Operator copy a bit if it is set in one operand but not both and assign the value to s3[] array variable. Using if else condition statement print the binary addition of strings.
$ cc bit20.c $ a.out enter string1 00010001 enter string2 00010010 binary addition of number is 000100011
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books