Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

add two large numbers

public static string AddTwoNumber(string x, string y)
{
    var X = x.ToCharArray().ToList().Select(s => Convert.ToInt32(s.ToString())).ToList();
    var Y = y.ToCharArray().ToList().Select(s => Convert.ToInt32(s.ToString())).ToList();

    X.Reverse();
    Y.Reverse();

    if (X.Count > Y.Count)
    {
        return Add(X, Y);
    }
    else
    {
        return Add(Y, X);
    }
}
public static string Add(List<int> first, List<int> second)
{
    var remainder = 0;
    var list = new List<int>();
    for (int i = 0; i < first.Count; i++)
    {
        var temp = 0;
        if(i<second.Count)
        {
            temp = second[i];
        }
        var num = temp + first[i] + remainder;
        var inString = num.ToString();

        if (inString.Length > 1)
        {
            list.Add(Convert.ToInt32(inString[1].ToString()));
            remainder = Convert.ToInt32(inString[0].ToString());
        }
        else
        {
            list.Add(Convert.ToInt32(inString[0].ToString()));
            remainder = 0;
        }
    }
    list.Reverse();
    var str = string.Empty;
    foreach (var item in list)
    {
        str += item;
    }
    return str;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: search list for words c# 
Csharp :: closedxm Iworksheet excel formulal 
Csharp :: how to preset an array c# 
Csharp :: Service Locator, Unity 
Csharp :: Adding number of day remaining to future date from now 
Csharp :: google sheets problems cell not considered even 
Csharp :: button next for picturebox c# 
Csharp :: c# how to load type of class from string 
Csharp :: Difference between PhotonNetwork.Ismasterclient and PhotonView.ismasterclient 
Csharp :: disable quickedit c# 
Csharp :: wpf onpropertychanged not working 
Csharp :: c# gridview summary item displayformat 
Csharp :: c# print 1 to 100 
Csharp :: count split elements .net 
Csharp :: two question marks together mean in C# 
Csharp :: C# enum fglag 
Csharp :: How to enumerate an enum 
Csharp :: Handling aggregation responses with NEST c# 
Csharp :: what loops are entry controlled c# 
Csharp :: www.elking.net 
Csharp :: using mediamanager how to play mp3 files 
Csharp :: unity create 
Csharp :: How to convert output of HttpClient PostAsJsonAsync() into user defined list of object 
Csharp :: Set property of control on form by name 
Csharp :: get path revit linked unload 
Csharp :: how to check if every element in array is true c# 
Csharp :: how to get the screen size in Tao.Freeglut 
Csharp :: dapper query list of parameters 
Csharp :: user input in c# 
Csharp :: .net core string compare ignore case and accents 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =