Dianov

For Loop - Lab (10. Odd Even Sum)

Dec 26th, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 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. namespace OddEvenSum
  8.  
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int n = int.Parse(Console.ReadLine());
  15.             int evenSum = 0;
  16.             int oddSum = 0;
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 int num = int.Parse(Console.ReadLine());
  20.                 if (i % 2 == 0)
  21.                 {
  22.                     evenSum += num;
  23.                 }
  24.                 else if (i % 2 != 0)
  25.                 {
  26.                     oddSum += num;
  27.                 }
  28.             }
  29.             if (evenSum == oddSum)
  30.             {
  31.                 Console.WriteLine("Yes");
  32.                 Console.WriteLine($"Sum = {evenSum}");
  33.             }
  34.             else if (evenSum != oddSum)
  35.             {
  36.                 Console.WriteLine("No");
  37.                 Console.WriteLine($"Diff = {Math.Abs(evenSum - oddSum)}");
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment