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
- برمجة سي شارب
- برمجة
- 2021-05-31
- ahmadghneem
الأجوبة
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();
}
}
}أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال