File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Program : To add 2 string
3
+
4
+ this Program is Contributed by github@b419007
5
+ */
6
+
7
+ #include < iostream>
8
+ using namespace std ;
9
+
10
+ int Len (string &str1, string &str2)
11
+ {
12
+ int len1 = str1.size ();
13
+ int len2 = str2.size ();
14
+ if (len1 < len2)
15
+ {
16
+ for (int i = 0 ; i < len2 - len1 ; i++)
17
+ str1 = ' 0' + str1;
18
+ return len2;
19
+ }
20
+ else if (len1 > len2)
21
+ {
22
+ for (int i = 0 ; i < len1 - len2 ; i++)
23
+ str2 = ' 0' + str2;
24
+ }
25
+ return len1;
26
+ }
27
+
28
+ string add ( string a, string b )
29
+ {
30
+ string result;
31
+ int len = Len (a, b);
32
+
33
+ int carry = 0 ;
34
+ for (int i = len-1 ; i >= 0 ; i--)
35
+ {
36
+ int aBit = a.at (i) - ' 0' ;
37
+ int bBit = b.at (i) - ' 0' ;
38
+ int sum = (aBit ^ bBit ^ carry)+' 0' ;
39
+
40
+ result = (char )sum + result;
41
+ carry = (aBit & bBit) | (bBit & carry) | (aBit & carry);
42
+ }
43
+
44
+ if (carry)
45
+ result = ' 1' + result;
46
+
47
+ return result;
48
+ }
49
+
50
+ int main ()
51
+ {
52
+ string str1,str2;
53
+ cout<<" Enter the string 1 :" ;
54
+ cin>>str1;
55
+ cout<<" Enter the string 2 :" ;
56
+ cin>>str2;
57
+ cout << " Sum is " << add (str1, str2);
58
+ return 0 ;
59
+ }
You can’t perform that action at this time.
0 commit comments