# C# Program to Find the Sum of Two Binary Numbers

This is a C# Program to find the sum of two binary numbers.

Problem Description

This C# Program Finds the Sum of two Binary Numbers.

Problem Solution

Here Binary number is a number that can be represented using only two numeric symbols – 0 and 1. A number in base 2.

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 executed with Microsoft Visual Studio. The program output is also shown below.

```/*
* C# Program to Find the Sum of two Binary Numbers */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int b1, b2;
int i = 0, rem = 0;
int[] sum = new int[20];
Console.WriteLine("Enter the first binary number: ");
Console.WriteLine("Enter the second binary number: ");
while (b1 != 0 || b2 != 0)
{
sum[i++] = (b1 % 10 + b2 % 10 + rem) % 2;
rem = (b1 % 10 + b2 % 10 + rem) / 2;
b1 = b1 / 10;
b2 = b2 / 10;
}
if (rem != 0)
sum[i++] = rem;
--i;
Console.WriteLine("Sum of two binary numbers: ");
while (i >= 0)
Console.Write("{0}", sum[i--]);
}
}
}```
Program Explanation

This C# program we are reading the first and second binary numbers using ‘b1’ and ‘b2’ variables respectively. Here Binary number is a number that can be represented using only two numeric symbols 0 and 1. Using while loop check the condition that both the values of ‘b1’ or ‘b2’variables are not equal to 0.

If the condition is true then execute the iteration of the loop. Compute the modulus of the value of ‘b1’ variable by 10 and the value of ‘b2’ variable by 10. Compute the summation of both the values. Divide the resulted value by 2.

Divide the value of ‘b1’ variable by 10 and the value of ‘b2’ variable by 10. If condition statement is used to check that the value of ‘rem’ variable is not equal to 0. If the condition is true, then execute the statement. Print the sum of two binary numbers.

Runtime Test Cases
```
Enter the first binary number:
100
Enter the second binary number:
110
Sum of two binary numbers:
1010```

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]