Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _06.Sum_big_numbers
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input1 = Console.ReadLine();
- string input2 = Console.ReadLine();
- string shortestNum = DetermineShortestNum(input1, input2);
- string longestNum = DetermineLongestNum(input1, input2);
- int sum = 0;
- int count = 0;
- List<string> sumString = new List<string>();
- for (int i = 0; i < shortestNum.Length; i++)
- {
- sum = (Convert.ToInt32(shortestNum[shortestNum.Length - 1 - i].ToString()) + Convert.ToInt32(longestNum[longestNum.Length - 1 - i].ToString()) + count);
- count = 0;
- if (sum >= 10)
- {
- sum %= 10;
- count++;
- }
- sumString.Add(sum.ToString());
- }
- if (count != 0)
- {
- int dif = longestNum.Length - shortestNum.Length;
- int lastCount = Convert.ToInt32(longestNum[longestNum.Last()].ToString()) + count;
- sumString.Add(lastCount.ToString());
- if (shortestNum.Length != longestNum.Length)
- {
- sumString.Add(new string(longestNum.Take(dif - 1).ToArray()));
- }
- }
- else
- {
- if (shortestNum.Length != longestNum.Length)
- {
- int dif = longestNum.Length - shortestNum.Length;
- sumString.Add(new string(longestNum.Take(dif).ToArray()));
- }
- }
- sumString.Reverse();
- Console.WriteLine(string.Join("", sumString));
- }
- static string DetermineShortestNum(string num1, string num2)
- {
- if (num1.Length != num2.Length)
- {
- if (num1.Length == Math.Min(num1.Length, num2.Length))
- {
- return num1;
- }
- else
- {
- return num2;
- }
- }
- else
- {
- return num1;
- }
- }
- static string DetermineLongestNum(string num1, string num2)
- {
- if (num1.Length != num2.Length)
- {
- if (num1.Length == Math.Max(num1.Length, num2.Length))
- {
- return num1;
- }
- else
- {
- return num2;
- }
- }
- else
- {
- return num2;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement