bullit3189

Sum Big Numbers - String and Text Processing

Feb 25th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace _06SumBigNumbers
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string num1 = Console.ReadLine().TrimStart('0');
  11. string num2 = Console.ReadLine().TrimStart('0');
  12.  
  13. int onMind = 0;
  14.  
  15. StringBuilder result = new StringBuilder();
  16.  
  17. if (num1.Length>num2.Length)
  18. {
  19. num2 = num2.PadLeft(num1.Length, '0');
  20. }
  21. else
  22. {
  23. num1 = num1.PadLeft(num2.Length, '0');
  24. }
  25.  
  26. for (int i = num1.Length - 1; i >= 0; i--)
  27. {
  28. char currSymbol1 = num1[i];
  29. char currSymbol2 = num2[i];
  30.  
  31. int num1Digit = int.Parse(currSymbol1.ToString());
  32. int num2Digit = int.Parse(currSymbol2.ToString());
  33.  
  34. int sum = num1Digit + num2Digit + onMind;
  35.  
  36. int lastDigit = sum % 10;
  37.  
  38. result.Append(lastDigit);
  39. onMind = sum / 10;
  40.  
  41. }
  42.  
  43. if (onMind > 0)
  44. {
  45. result.Append(onMind);
  46. }
  47.  
  48. StringBuilder answer = new StringBuilder();
  49.  
  50. for (int i = result.Length - 1; i >= 0; i--)
  51. {
  52. char curr = result[i];
  53. answer.Append(curr);
  54. }
  55.  
  56. Console.WriteLine(answer.ToString());
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment