-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADD.cpp
More file actions
90 lines (55 loc) · 2.34 KB
/
Copy pathADD.cpp
File metadata and controls
90 lines (55 loc) · 2.34 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<bits/stdc++.h>
using namespace std;
string add(string s , string t)
{
string p =""; // intialising empty string to generate final output and to add each character to it.
int i = s.length()-1,j=t.length()-1; // starting from end of the string.
int c = 0; // carry for addition.
while(i>=0&&j>=0) // this loop comes from last and does addition upto same length.
{
int si = s[i] - '0';// converting to int.
int tj = t[j] - '0';
int sum = si + tj + c; // intially carry = 0;
c = sum/10; // new carry
sum = sum%10; // neglecting carry as carry is required next time.
char v = (sum+'0');
p = v + p; // adding to string to generate final o/p.
i--;
j--;
}
while(i>=0)// this loop does remaining addition if 2 integers are of diiferent length
{
int si = s[i] - '0';// converting to int.
int sum = si + c; // doing sum with carry.
c = sum/10;// new carry
sum = sum%10; // neglecting carry as carry is required next time.
char v = (sum+'0'); // converting added value to char .
p = v + p; // adding to string to generate final o/p.
i--;
}
while(j>=0)// this loop does remaining addition if 2 integers are of diiferent length
{
int tj = t[j] - '0';// converting to int.
int sum = tj + c;// doing sum with carry.
c = sum/10; // new carry
sum = sum%10; // neglecting carry as carry is required next time.
char v = (sum+'0'); // converting added value to char .
p = v + p; // adding to string to generate final o/p.
j--;
}
if(c!=0) // adding carry (if carry is not equal to zero because we dont add leading zeroes)
{
char v = c + '0'; // converting carry to char.
p = v + p;// adding carry to string.
}
return p; // return final o/p as string.
}
int main()
{
string s,t;
printf("Enter first Integer : "); // as integer is large , taking input as string .
cin >> s;
printf("Enter second Integer : ");
cin >> t;
cout << add(s,t) << endl;
}