WindFell

Sum Big Numbers

May 16th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class SumBigNumbers
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         string firstWord = Console.ReadLine();
  9.         string secondWord = Console.ReadLine();
  10.  
  11.         Sum(firstWord, secondWord);
  12.     }
  13.  
  14.     static void Sum(string firstWord, string secondWord)
  15.     {
  16.         StringBuilder builder = new StringBuilder();
  17.  
  18.         int maxLenth = Math.Max(firstWord.Length, secondWord.Length);
  19.         int remainder = 0;
  20.  
  21.         firstWord = firstWord.PadLeft(maxLenth, '0');
  22.         secondWord = secondWord.PadLeft(maxLenth, '0');
  23.  
  24.         for (int index = maxLenth - 1; index >= 0; index--)
  25.         {
  26.             int sum = firstWord[index] - 48 + secondWord[index] - 48 + remainder;
  27.             builder.Insert(0, sum % 10);
  28.             remainder = sum / 10;
  29.  
  30.             if (index == 0)
  31.             {
  32.                 builder.Insert(0, remainder);
  33.             }
  34.         }
  35.  
  36.         string result = string.Join("", builder.ToString().TrimStart('0'));
  37.         Console.WriteLine(result);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment