forked from DeepanshuProgrammer/GeeksforGeeks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20 August "Problem of the Day" Answer
63 lines (59 loc) · 1.49 KB
/
20 August "Problem of the Day" Answer
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
GeeksforGeeks
The Question is :- "Sum of two large numbers"
Answer :-
class Solution {
public:
string findSum(string X, string Y)
{
if(X.length()<Y.length())
{
int n=Y.length()-X.length();
for(int i=0;i<n;i++)
{
X='0'+X;
}
}
else if(X.length()>Y.length())
{
int n=X.length()-Y.length();
for(int i=0;i<n;i++)
{
Y='0'+Y;
}
}
int carry=0,n=X.length();
string ans;
for(int i=n-1;i>=0;i--)
{
int num1=X[i]-'0',num2=Y[i]-'0';
int sum=carry+num1+num2;
int sum1=sum%10;
carry=sum/10;
char digit='0'+sum1;
ans=digit+ans;
}
if(carry>0)
{
char car='0'+carry;
ans=car+ans;
}
int isDigit=-1;
string finalAns;
for(int i=0;i<ans.length();i++)
{
if(ans[i]!='0' || i==n-1)
{
isDigit=i;
break;
}
}
for(int i=isDigit;i<ans.length();i++)
{
finalAns=finalAns+ans[i];
}
return finalAns;
}
};
Hope you understand the answer, and complete it.
Stay Connected for daily Problem of the Day answers.
Thank you All!