-
Notifications
You must be signed in to change notification settings - Fork 13
/
exo 30.cs
60 lines (48 loc) · 1.27 KB
/
exo 30.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
public class Class1
{
static void Main(string[] args)
{
string num1 = "12";
string num2 = "198111";
Console.WriteLine(findSum(num1, num2));
}
static string findSum(string str1, string str2)
{
if (str1.Length > str2.Length)
{
string t = str1;
str1 = str2;
str2 = t;
}
string str = "";
int n1 = str1.Length, n2 = str2.Length;
char[] ch = str1.ToCharArray();
Array.Reverse(ch);
str1 = new string(ch);
char[] ch1 = str2.ToCharArray();
Array.Reverse(ch1);
str2 = new string(ch1);
int carry = 0;
for (int i = 0; i < n1; i++)
{
int sum = ((int)(str1[i] - '0') + (int)(str2[i] - '0') + carry);
str += (char)(sum % 10 + '0');
carry = sum / 10;
}
for (int i = n1; i < n2; i++)
{
int sum = ((int)(str2[i] - '0') + carry);
str += (char)(sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0)
{
str += (char)(carry + '0');
}
char[] ch2 = str.ToCharArray();
Array.Reverse(ch2);
str = new string(ch2);
return str;
}
}