Advertisement
Alexander7337

BigNums

May 31st, 2016
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class AdditionAlgorithm
  5. {
  6.     static void Main()
  7.     {
  8.         string a = Console.ReadLine().Trim();
  9.         string b = Console.ReadLine().Trim();
  10.  
  11.         Console.WriteLine(Add(a, b));
  12.  
  13.     }
  14.  
  15.     public static string Add(string a, string b)
  16.     {
  17.         int maxLen = Math.Max(a.Length, b.Length);
  18.         a = a.PadLeft(maxLen + 1, '0');
  19.         b = b.PadLeft(maxLen + 1, '0');
  20.  
  21.         int[] arr1 = a.Select(x => int.Parse(x.ToString())).ToArray();
  22.         int[] arr2 = b.Select(x => int.Parse(x.ToString())).ToArray();
  23.         int[] sum = new int[arr1.Length];
  24.  
  25.         int carry = 0;
  26.         for (int i = sum.Length - 1; i >= 0; i--)
  27.         {
  28.             int total = arr1[i] + arr2[i] + carry;
  29.             sum[i] = total % 10;
  30.             if (total > 9) carry = 1;
  31.             else carry = 0;
  32.         }
  33.         return string.Join("", sum.SkipWhile(x => x == 0));
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement