Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- using System.Collections.Generic;
- class SumTwoIntsPresentedInArrays
- {
- static void PrintSumOfArrays(List<byte> arrSum)
- {
- Console.WriteLine("The sum ot the numbers is: ");
- foreach (var number in arrSum)
- {
- Console.Write(number);
- }
- Console.WriteLine();
- }
- static List<byte> SumArrays(byte[] array1, byte[] array2)
- {
- byte transferValue = 0;
- byte tempSum = 0;
- List<byte> arrSum = new List<byte>();
- for (int i = 0; i < array1.Length; i++)
- {
- array1[i] += transferValue; //Carry over 1 or 0 to first value
- transferValue = (byte)((array1[i] + array2[i]) / 10); //divide the sum at 10 and get 1 or 0, (sum range 0-18)
- tempSum = (byte)((array1[i] + array2[i]) % 10); //Divide for reminder at 10 and get the value
- arrSum.Add(tempSum); //Put that value into the list
- }
- arrSum.Reverse();
- if (arrSum[0] == 0)
- arrSum.Remove(0);
- return arrSum;
- }
- static byte[] PutStringNumberInArray(string strNumber, int maxLenght)
- {
- maxLenght = maxLenght + 1;
- byte[] arrayNum = new byte[maxLenght];
- for (int i = strNumber.Length; i > 0; i--)
- {
- arrayNum[strNumber.Length - i] = byte.Parse(strNumber[i - 1].ToString());
- }
- return arrayNum;
- }
- static void Main()
- {
- Console.WriteLine("First number");
- string numberA = Console.ReadLine();
- Console.WriteLine("Second number");
- string numberB = Console.ReadLine();
- int longerNum = Math.Max(numberA.Length, numberB.Length);
- byte[] firstArr = PutStringNumberInArray(numberA, longerNum);
- byte[] seconfArr = PutStringNumberInArray(numberB, longerNum);
- List<byte> arraySum = SumArrays(firstArr, seconfArr);
- PrintSumOfArrays(arraySum);
- //BigInteger first = BigInteger.Parse(numberA); //Just for check
- //BigInteger second = BigInteger.Parse(numberB);
- //Console.WriteLine("Sum with BigInteger:");
- //Console.WriteLine(first + second);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement