How can I add two long integers passed as a string? c #

1

First of all, I want to thank you for your time when it comes to reading my question. I am new to c # and in programming in general, and I am having problems with something simple. I want to add two long integers as a string of text, I have reached the point where I break down the chains and store them in two arrays of a dimension but then I am not able to add both and receive a coherent result. Greetings Isra.

Edit 1

I add the code where I have already surrendered ....

' static void Main(string[] args)
    {
        string num1, num2;
        int[] numeros1;
        int[] numeros2;
        int suma = 0;

        Console.WriteLine("Introduce un numero largo: ");
        num1 = Console.ReadLine();

        Console.WriteLine("Introduce un numero largo: ");
        num2 = Console.ReadLine();

        numeros1 = new int[num1.Length];

        for(int x = 0; x < num1.Length-1; x++)
        {
            numeros1[x] = num1[x];
        }

        numeros2 = new int[num2.Length];

        for (int x = 0; x < num1.Length - 1; x++)
        {
            numeros2[x] = num2[x];
        }

        for (int x = 0; x < numeros1.Length; x++)
        {
            for (int y = 0; y < numeros2.Length; y++)
            {
                suma += (numeros1[x] + numeros2[y]);
            }
        }

        Console.WriteLine("La suma de las dos cadenas introducidas es: {0}", suma);
        Console.ReadKey();
    }'
    
asked by Israel miguel saura 06.07.2018 в 20:25
source

1 answer

3

In C # there are some functions that help you convert. Because you want to place long strings I recommend you use the Int64.Parse (string s) function. In C #, as in some other languages, you can not add strings (with the +), the + operator will work to concatenate the strings you want to "add". So first you have to convert them and then add them up.

        ...
        string num1 = "", num2 = "";
        double suma = 0;

        Console.WriteLine("Introduce un numero largo: ");
        num1 = Console.ReadLine();

        Console.WriteLine("Introduce un numero largo: ");
        num2 = Console.ReadLine();

        suma = Int64.Parse(num1) + Int64.Parse(num2); //Aquí es la parte donde convierto las cadenas largas en enteros para poderlas sumar.
        Console.WriteLine("La suma de las dos cadenas introducidas es: {0}", suma);
        Console.ReadKey();
        ...
    
answered by 06.07.2018 в 21:35