C Program to Add Two Binary Strings

«
»

This is a C Program to perform binary addition of strings and print it.

Problem Description

This C Program Performs Binary Addition of strings and Print it.

Problem Solution

Take input from the user and performs binary addition as shown in the program below.

advertisement
Program/Source Code

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';
        }
    }
}
Program Explanation

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.

advertisement
advertisement

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.

Runtime Test Cases
 
$ 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.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.