Write a C# Sharp program to compute the sum of the positive and negative numbers of an array of integers and display the largest sum

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to compute the sum of the positive and negative numbers of an array of integers and display the largest sum. 
Expected Output:
Original array elements:
-10 -11 -12 -13 -14 15 16 17 18 19 20
Largest sum - Positive/Negative numbers of the said array: 105
Original array elements:
-11 -22 -44 0 3 4 5 9
Largest sum - Positive/Negative numbers of the said array: -77

الأجوبة

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = { -10, -11, -12, -13, -14, 15, 16, 17, 18, 19, 20 };
            Console.WriteLine("Original array elements:");
            for (int i = 0; i < nums.Length; i++)
            {
                Console.Write(nums[i] + " ");
            }
            Console.WriteLine("\nLargest sum - Positive/Negative numbers of the said array: " + test(nums));
            int[] nums1 = { -11, -22, -44, 0, 3, 4 , 5, 9 };
            Console.WriteLine("\nOriginal array elements:");
            for (int i = 0; i < nums1.Length; i++)
            {
                Console.Write(nums1[i] + " ");
            }
            Console.WriteLine("\nLargest sum - Positive/Negative numbers of the said array: " + test(nums1));
        }
        public static int test(int[] nums)
        {
            int[] sums = { nums.Where(x => x > 0).Sum(),nums.Where(x => x < 0).Sum() };
            return sums.OrderByDescending(x => Math.Abs(x)).First();
        }
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...