Advertisement
YavorJS

6. Sum big numbers

Oct 19th, 2016
1,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string n1 = Console.ReadLine().TrimStart(new char[] { '0' });
  13. string n2 = Console.ReadLine().TrimStart(new char[] { '0' });
  14. StringBuilder result = new StringBuilder();
  15. if (n1.Length > n2.Length)
  16. {
  17. n2 = n2.PadLeft(n1.Length, '0');
  18. }
  19. else if (n1.Length < n2.Length)
  20. {
  21. n1 = n1.PadLeft(n2.Length, '0');
  22. }
  23. char[] str1 = n1.ToCharArray();
  24. char[] str2 = n2.ToCharArray();
  25.  
  26. sbyte remainder = 0;
  27. sbyte addition = 0;
  28. for (int i = str1.Length - 1; i >= 0; i--)
  29. {
  30. sbyte num1 = sbyte.Parse(str1[i].ToString());
  31. sbyte num2 = sbyte.Parse(str2[i].ToString());
  32. num1 += addition;
  33. addition = 0;
  34. if (num1+num2<10)
  35. {
  36. result.Append(num1 + num2);
  37. }
  38. else
  39. {
  40. remainder = (sbyte)((num1 + num2) % 10);
  41. result.Append(remainder);
  42. addition= (sbyte)((num1 + num2) /10);
  43. }
  44. }
  45. if (addition!=0)
  46. {
  47. result.Append(addition);
  48. }
  49.  
  50. char[] endResult=result.ToString().ToCharArray();
  51. Array.Reverse(endResult);
  52.  
  53. Console.WriteLine(string.Join("",endResult));
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement