logo
  • Home
  • Rank
  • Tests
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program to Find the Sum of two Binary Numbers

Posted on February 14, 2013 by staff10

This is a C program to Find the Sum of two Binary Numbers.

Problem Description

This program finds the sum of two binary numbers.

Problem Solution

1. Take two binary numbers as input.
2. Add each bits from the two binary numbers separately starting from LSB.
3. The operations may be as follows.
a) (0+0)=0,
b) (1+0)=1,
c) (1+1)=0 and 1 will be remainder.

advertisement
Program/Source Code

Here is source code of the C program to Find the Sum of two Binary Numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find the Sum of two Binary Numbers
  3.  */
  4. #include <stdio.h>
  5.  
  6. int main()
  7. {
  8.  
  9.     long binary1, binary2;
  10.     int i = 0, remainder = 0, sum[20];
  11.  
  12.     printf("Enter the first binary number: ");
  13.     scanf("%ld", &binary1);
  14.     printf("Enter the second binary number: ");
  15.     scanf("%ld", &binary2);
  16.     while (binary1 != 0 || binary2 != 0)
  17.     {
  18.         sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
  19.         remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
  20.         binary1 = binary1 / 10;
  21.         binary2 = binary2 / 10;
  22.     }
  23.     if (remainder != 0)
  24.         sum[i++] = remainder;
  25.     --i;
  26.     printf("Sum of two binary numbers: ");
  27.     while (i >= 0)
  28.         printf("%d", sum[i--]);
  29.     return 0;
  30. }
Program Explanation

1. Take two binary numbers as input and store it in the variables binary1 and binary2.
2. Initialize the variables i and remainder to zero.
3. Obtain the remainders of both the binary numbers.
4. Obtain the quotients of both the binary numbers.
5. Firstly add the remainders of both binary numbers and further add the variable remainder.
6. Obtain the remainder of the result got at step 5 when divided by 2 and store it in the array sum[].
7. Obtain the quotient of the result got at step 5 when divided by 2 and override the variable remainder with this value.
8. Override the variables binary1 and binary2 with their quotient got at step 4.
9. Repeat the steps 3-8 with the new values of binary1 and binary2 until both becomes zero.
10. When it becomes zero check if any remainder exits. If it is, then copy it into the array sum.
11. Print the sum as output.

advertisement
Runtime Test Cases
Output:
 
Enter the first binary number: 100000
Enter the second binary number: 101010
Sum of two binary numbers: 1001010

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Check whether a given Number is Perfect Number
» Next Page - C Program to Find the Sum of A.P Series

« C Program to Check whether a given Number is Perfect Number
C Program to Find the Sum of A.P Series »

Deep Dive @ Sanfoundry:

  1. C# Programming Examples on Mathematics
  2. C Programming Examples
  3. C# Programming Examples on LINQ
  4. C# Programming Examples
  5. C Programming Examples on Combinatorial Problems & Algorithms
  6. Simple C Programs
  7. C Programming Examples on Bitwise Operations
  8. C# Programming Examples on Conversions
  9. C Programming Examples using Recursion
  10. C Programming Examples without using Recursion
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.