Please click here for running code
using System;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string str1 = "123123123123123123213123",
str2 = "212321312312312312312312312321312312312312",
strResult;
StrLongInteger v1 = new StrLongInteger(str1);
StrLongInteger v2 = new StrLongInteger(str2);
strResult = (v1 + v2).ToString();
Console.WriteLine("Sum of Two number: " + strResult);
}
}
public class StrLongInteger
{
private string strNumber;
public StrLongInteger(string numberAsString)
{
this.strNumber = numberAsString;
}
// Adds two string long numbers.
public static StrLongInteger operator +(StrLongInteger strLI1, StrLongInteger strLI2)
{
char[] outputChars = new char[500];
string longStr1 = Reverse(strLI1.ToString());
string longStr2 = Reverse(strLI2.ToString());
int length1 = longStr1.Length;
int length2 = longStr2.Length;
int maxLength = length1 > length2 ? length1 : length2;
int carry = 0;
int j = 0;
for (j = 0; j < maxLength; j++)
{
int digit1 = (j > length1 - 1) ? 0 : Convert.ToInt32(longStr1[j].ToString());
int digit2 = (j > length2 - 1) ? 0 : Convert.ToInt32(longStr2[j].ToString());
int digitSum = digit1 + digit2 + carry;
if (digitSum >= 10)
{
digitSum -= 10;
carry = 1;
}
else
carry = 0;
outputChars[j] = Convert.ToChar(Convert.ToString(digitSum));
}
if (carry == 1)
outputChars[j++] = '1';
return new StrLongInteger(Reverse(new string(outputChars)));
}
// reverses the string.
private static string Reverse(string strNumber)
{
int j = 0;
char[] outputChars = new char[strNumber.Length];
for (int i = strNumber.Length - 1; i >= 0; i--)
{
outputChars[j] = strNumber[i];
j++;
}
return new string(outputChars);
}
public override string ToString()
{
return strNumber.TrimStart('\0');
}
}
}
//Output
//Sum of Two number:212321312312312312435435435444435435525435
using System;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string str1 = "123123123123123123213123",
str2 = "212321312312312312312312312321312312312312",
strResult;
StrLongInteger v1 = new StrLongInteger(str1);
StrLongInteger v2 = new StrLongInteger(str2);
strResult = (v1 + v2).ToString();
Console.WriteLine("Sum of Two number: " + strResult);
}
}
public class StrLongInteger
{
private string strNumber;
public StrLongInteger(string numberAsString)
{
this.strNumber = numberAsString;
}
// Adds two string long numbers.
public static StrLongInteger operator +(StrLongInteger strLI1, StrLongInteger strLI2)
{
char[] outputChars = new char[500];
string longStr1 = Reverse(strLI1.ToString());
string longStr2 = Reverse(strLI2.ToString());
int length1 = longStr1.Length;
int length2 = longStr2.Length;
int maxLength = length1 > length2 ? length1 : length2;
int carry = 0;
int j = 0;
for (j = 0; j < maxLength; j++)
{
int digit1 = (j > length1 - 1) ? 0 : Convert.ToInt32(longStr1[j].ToString());
int digit2 = (j > length2 - 1) ? 0 : Convert.ToInt32(longStr2[j].ToString());
int digitSum = digit1 + digit2 + carry;
if (digitSum >= 10)
{
digitSum -= 10;
carry = 1;
}
else
carry = 0;
outputChars[j] = Convert.ToChar(Convert.ToString(digitSum));
}
if (carry == 1)
outputChars[j++] = '1';
return new StrLongInteger(Reverse(new string(outputChars)));
}
// reverses the string.
private static string Reverse(string strNumber)
{
int j = 0;
char[] outputChars = new char[strNumber.Length];
for (int i = strNumber.Length - 1; i >= 0; i--)
{
outputChars[j] = strNumber[i];
j++;
}
return new string(outputChars);
}
public override string ToString()
{
return strNumber.TrimStart('\0');
}
}
}
//Output
//Sum of Two number:212321312312312312435435435444435435525435