using System; namespace Program { class Program { static void Main(string[] args) { string binary1, binary2; int sum = 0; Console.Write("Enter first binary number: "); binary1 = Console.ReadLine(); Console.Write("Enter second binary number: "); binary2 = Console.ReadLine(); int a = Convert.ToInt32(binary1, 2); int b = Convert.ToInt32(binary2, 2); sum = a + b; Console.WriteLine("\nDecimal: {0} ", sum + " \nBinary: " + Convert.ToString(sum, 2).PadLeft(8, '0')); Console.ReadLine(); } } }
Output
Notes
using System; 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: "); b1 = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the second binary number: "); b2 = int.Parse(Console.ReadLine()); 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--]); Console.ReadLine(); } } }