Answered How to add numbers of 1000 digits

  • Thursday, April 12, 2012 2:57 PM
     
      Has Code

    I am trying to write a function which will take two parameters as character array (these will be the two numbers <= 1000 digits long). It will return the result as a character array. Suppose I name this function "Add". So I want to be able to do this:

    char[] first = new char[1000];
    for(int i = 0;i<1000;i++)
    {
            answer[i]= Console.ReadLine();
    }
    
    char[] second = new char[500];
    for(int i = 0;i<500;i++)
    {
            second[i]= Console.ReadLine();
    }
    
    char[] result = Add(first, second);
    Console.WriteLine();
    for(int i = 0;i<result.length-1;i++)
    {
            Console.Write(result[i]);
    }
    


    -k@N@k-

All Replies

  • Thursday, April 12, 2012 3:03 PM
     
     

    Do you want to add them numerically, i.e. if the first array is {1, 2} and the second is {1} it will return {1,3} or concat the arrays such that it would return {1,2,1} for those inputs?

    If you need to deal with large numbers I highly suggest you just work with BigInteger.  It will be much easier, it will be more efficient (in both space and time), it will be less error prone, etc.

  • Thursday, April 12, 2012 3:05 PM
     
      Has Code

    You should do it as learned in school:

    123456789
       123456
        1111 
    ---------
    123580245
    

    Or use BigInteger. Take a look at BigInt aslo.

  • Thursday, April 12, 2012 3:17 PM
     
     Answered Has Code

    If your

          BigInteger n1 = BigInteger.Parse(Console.ReadLine());
          n1 += BigInteger.Parse(Console.ReadLine());
          Console.WriteLine(n1);
          Console.ReadLine();
    

    digits are base 10 numbers, use BigInteger:

  • Thursday, April 12, 2012 6:05 PM
     
     

    Do you want to add them numerically, i.e. if the first array is {1, 2} and the second is {1} it will return {1,3} or concat the arrays such that it would return {1,2,1} for those inputs?

    If you need to deal with large numbers I highly suggest you just work with BigInteger.  It will be much easier, it will be more efficient (in both space and time), it will be less error prone, etc.

    i want to add them numerically to get {1,3}

    -k@N@k-

  • Thursday, April 12, 2012 6:30 PM
     
     

    Then use big integer, for all of the reasons I mentioned before.

    If you really want to do it yourself, as mentioned by another reader, the method is more or less the same as what you learned in grade school.  Start at the least significant digit of each number and add them.  Remember the carry.  While you have more digits or a carry add the next pair of digits and the carry.  The algorithm is that straightforward. (if you don't care about performance in the least).

  • Friday, April 13, 2012 6:45 AM
    Moderator
     
     Answered Has Code

    Hi Alam,

      I feel you need to change your code because char array doesn't read the character from the result of Console.ReadLine().So I have changed your code as follows:

     
                string[] first = new string[1000];
                string[] answer=new string[100];
                for (int i = 0; i < 1000; i++)
                {
                    answer[i] = Console.ReadLine();
                }
    
                string[] second = new string[500];
                for (int i = 0; i < 500; i++)
                {
                    second[i] = Console.ReadLine();
                }
    
                string[] result = Add(first, second);
                Console.WriteLine();
                for (int i = 0; i < result.Length - 1; i++)
                {
                    Console.Write(result[i]);
                }

    Sincerely,

    Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us