Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace SumEvenNumbers
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] num = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToArray();
- int sumEven = 0;
- int sumOdd = 0;
- for (int i = 0; i < num.Length; i++)
- {
- int current = num[i];
- if (current % 2 == 0)
- {
- sumEven += current;
- }
- else
- {
- sumOdd += current;
- }
- }
- Console.WriteLine(sumEven - sumOdd);
- }
- }
- }
- Решение с LINQ:
- using System;
- using System.Linq;
- namespace SumEvenNumbers
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int sumEven = numbers.Where(a => a % 2 == 0).Sum();
- int sumOdd = numbers.Where(a => a % 2 == 1).Sum();
- Console.WriteLine(sumEven - sumOdd);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement