Advertisement
Guest User

Half Sum

a guest
May 27th, 2015
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class HalfSum
  5. {
  6.     static void Main()
  7.     {
  8.         byte n = byte.Parse(Console.ReadLine());
  9.  
  10.         //declare an array storing all entered values
  11.         int[] enteredValues = new int[2*n];
  12.         //fill in the array propting the user for values
  13.         for (int i = 0; i < 2*n; i++)
  14.         {
  15.             enteredValues[i] = int.Parse(Console.ReadLine());
  16.         }
  17.        
  18.         //declare 2 new arrays. One for the first half of of the enteredValues array and another for the second half
  19.         int[] leftSide = enteredValues.Take(enteredValues.Length / 2).ToArray();
  20.         int[] rightSide = enteredValues.Skip(enteredValues.Length / 2).ToArray();
  21.  
  22.         int leftSideSum = leftSide.Sum();
  23.         int rightSideSum = rightSide.Sum();
  24.  
  25.         if (leftSideSum == rightSideSum)
  26.         {
  27.             Console.WriteLine("Yes, sum=" + leftSideSum);
  28.         }
  29.  
  30.         else if (leftSideSum > rightSideSum)
  31.         {
  32.             Console.WriteLine("No, diff=" + (leftSideSum-rightSideSum));
  33.         }
  34.         else
  35.         {
  36.             Console.WriteLine("No, diff=" + (rightSideSum-leftSideSum));
  37.         }
  38.        
  39.            
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement