nikolayneykov

Untitled

Feb 3rd, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.  
  9.         for (int i = 0; i < n; i++)
  10.         {
  11.             string numbers = Console.ReadLine();
  12.             int index = numbers.IndexOf(' ');
  13.             long leftNumber = long.Parse(numbers.Substring(0, index));
  14.             long rightNumber = long.Parse(numbers.Substring(index));
  15.  
  16.             long sum = (leftNumber > rightNumber) ?
  17.                 CalculateDigitSum(leftNumber) :
  18.                 CalculateDigitSum(rightNumber);
  19.  
  20.             Console.WriteLine(sum);
  21.         }
  22.     }
  23.     static long CalculateDigitSum(long number)
  24.     {
  25.         long sum = 0;
  26.         while (number != 0)
  27.         {
  28.             sum += number % 10;
  29.             number /= 10;
  30.         }
  31.         return Math.Abs(sum);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment