Advertisement
Booster

Sum Two Numbers Up To 10000 Digits Presented in Arrays

Aug 6th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. using System.Collections.Generic;
  4.  
  5. class SumTwoIntsPresentedInArrays
  6. {
  7.     static void PrintSumOfArrays(List<byte> arrSum)
  8.     {
  9.         Console.WriteLine("The sum ot the numbers is: ");
  10.         foreach (var number in arrSum)
  11.         {
  12.             Console.Write(number);
  13.         }
  14.         Console.WriteLine();
  15.  
  16.     }
  17.     static List<byte> SumArrays(byte[] array1, byte[] array2)
  18.     {
  19.         byte transferValue = 0;
  20.         byte tempSum = 0;
  21.         List<byte> arrSum = new List<byte>();
  22.  
  23.         for (int i = 0; i < array1.Length; i++)
  24.         {                                                              
  25.             array1[i] += transferValue;                                 //Carry over 1 or 0 to first value
  26.             transferValue = (byte)((array1[i] + array2[i]) / 10);        //divide the sum at 10 and get 1 or 0, (sum range 0-18)
  27.             tempSum = (byte)((array1[i] + array2[i]) % 10);            //Divide for reminder at 10 and get the value
  28.             arrSum.Add(tempSum);                                       //Put that value into the list
  29.         }
  30.         arrSum.Reverse();
  31.         if (arrSum[0] == 0)
  32.             arrSum.Remove(0);
  33.         return arrSum;
  34.     }
  35.     static byte[] PutStringNumberInArray(string strNumber, int maxLenght)
  36.     {
  37.         maxLenght = maxLenght + 1;  
  38.         byte[] arrayNum = new byte[maxLenght];
  39.      
  40.         for (int i = strNumber.Length; i > 0; i--)
  41.         {
  42.             arrayNum[strNumber.Length - i] = byte.Parse(strNumber[i - 1].ToString());
  43.         }
  44.         return arrayNum;
  45.     }
  46.     static void Main()
  47.     {
  48.         Console.WriteLine("First number");        
  49.         string numberA = Console.ReadLine();      
  50.         Console.WriteLine("Second number");
  51.         string numberB = Console.ReadLine();
  52.         int longerNum = Math.Max(numberA.Length, numberB.Length);
  53.  
  54.         byte[] firstArr = PutStringNumberInArray(numberA, longerNum);
  55.         byte[] seconfArr = PutStringNumberInArray(numberB, longerNum);
  56.  
  57.         List<byte> arraySum = SumArrays(firstArr, seconfArr);
  58.         PrintSumOfArrays(arraySum);
  59.  
  60.         //BigInteger first = BigInteger.Parse(numberA);                       //Just for check
  61.         //BigInteger second = BigInteger.Parse(numberB);
  62.         //Console.WriteLine("Sum with BigInteger:");
  63.         //Console.WriteLine(first + second);
  64.    
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement