Users Online

· Guests Online: 151

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

32 C Program to Find the Sum of two Binary Numbers

This C Program Calculates the Sum of two Binary Numbers. Binary number is a number that can be represented using only two numeric symbols – 0 and 1. A number in base 2.
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.

/*
* C Program to Find the Sum of two Binary Numbers
*/
#include <stdio.h>

int main()
{

long binary1, binary2;
int i = 0, remainder = 0, sum[20];

printf("Enter the first binary number: ");
scanf("%ld", &binary1);
printf("Enter the second binary number: ");
scanf("%ld", &binary2);
while (binary1 != 0 || binary2 != 0)
{
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);
return 0;
}

Output:
$ cc pgm9.c
$ a.out
Enter the first binary number: 100000
Enter the second binary number: 101010
Sum of two binary numbers: 1001010

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.78 seconds
10,817,172 unique visits