-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySum.cs
45 lines (35 loc) · 909 Bytes
/
binarySum.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
// To execute C#, please define "static void Main" on a class
// named Solution.
class Solution
{
static void Main(string[] args)
{
string a = "11";
string b = "1";
binarySum(a,b);
}
public static void binarySum(string a, string b)
{
int i = a.Length-1;
int j = b.Length-1;
int sum = 0;
string result = "";
while(i>=0 || j>=0 || sum ==1)
{
if(i>=0)
{
sum = sum + (a[i] - '0');
}
if(j>=0)
{
sum = sum + (b[j] - '0'); //convrting char to int, shortcut
}
result = (char)(sum % 2 + '0') + result;
sum = sum/2;
i--;
j--;
}
Console.WriteLine(result);
}
}