C Program to Input 2 Binary Strings and Print their Binary Sum

«
»
This C Program takes an input to 2 binary strings and print their binary sum.

Here is a source code of the C program to input 2 binary strings and print their binary sum. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Input 2 Binary Strings and Print their Binary 
  3.  * Sum 
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. int bin_verify(char []);
  10. void sum(char [], char [], char []);
  11.  
  12. int main()
  13. {
  14.     char bin1[33], bin2[33], result[33];
  15.     int len1, len2, check;
  16.  
  17.     printf("Enter binary number 1: ");
  18.     scanf("%s", bin1);
  19.     printf("Enter binary number 2: ");
  20.     scanf("%s", bin2);
  21.     check = bin_verify(bin1);
  22.     if (check)
  23.     {
  24.         printf("Invalid binary number %s.\n", bin1);
  25.         exit(0);
  26.     }
  27.     check = bin_verify(bin2);
  28.     if (check)
  29.     {
  30.         printf("Invalid binary number %s.\n", bin2);
  31.         exit(0);
  32.     }
  33.     sum(bin1, bin2, result);
  34.     printf("%s + %s = %s\n", bin1, bin2, result);
  35.  
  36.     return 0;
  37. }
  38.  
  39. int bin_verify(char str[])
  40. {
  41.     int i;
  42.  
  43.     for (i = 0; i < strlen(str); i++)
  44.     {
  45.         if ((str[i] - '0' != 1 ) && (str[i] - '0' != 0))
  46.         {
  47.             return 1;
  48.         }
  49.     }
  50.  
  51.     return 0;
  52. }
  53.  
  54. void sum(char bin1[], char bin2[], char result[])
  55. {
  56.     int i = strlen(bin1) - 1;
  57.     int j = strlen(bin2) - 1;
  58.     int carry = 0, temp, num1, num2;
  59.  
  60.     while (i > -1 && j > -1)
  61.     {
  62.         num1 = bin1[i] - '0';
  63.         num2 = bin2[j] - '0';
  64.         temp = num1 + num2 + carry;
  65.         if (temp / 2 == 1)
  66.         {
  67.             carry = 1;
  68.             temp %= 2;
  69.         }
  70.         if (i > j)
  71.         {
  72.             result[i + 1] = temp + '0';
  73.             result[strlen(bin1) + 1] = '\0';
  74.         }
  75.         else
  76.         {
  77.             result[j +1] = temp + '0';
  78.             result[strlen(bin2) + 1] = '\0';
  79.         }
  80.         i--;
  81.         j--;
  82.     }
  83.     while (i > -1)
  84.     {
  85.         temp = bin1[i] + carry - '0';
  86.         if (temp / 2 == 1)
  87.         {
  88.             carry = 1;
  89.             temp %= 2;
  90.         }
  91.         result[i + 1] = temp + '0';
  92.         i--;
  93.     }
  94.     while (j > -1)
  95.     {
  96.         temp = bin2[j] + carry - '0';
  97.         if (temp / 2 == 1)
  98.         {
  99.             carry = 1;
  100.             temp %= 2;
  101.         }
  102.         result[j + 1] = temp + '0';
  103.         j--;
  104.     }
  105.     if (carry)
  106.     {
  107.         result[0] = '1';
  108.     }
  109.     else
  110.     {
  111.         result[0] = '0';
  112.     }
  113. }

advertisement
$ gcc binarynum.c
$ ./a.out
Enter binary number 1: 0110 
Enter binary number 2: 1011
0110 + 1011 = 10001

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
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
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.