Write a C# Sharp program to calculate the sum of two lowest negative numbers of a given array of integers

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

Write a C# Sharp program to calculate the sum of two lowest negative numbers of a given array of integers.
An integer (from the Latin integer meaning "whole") is colloquially defined as a number that can be written without a fractional component. For example, 21, 4, 0, and −2048 are integers,
Test Data :
Original array elements:
-10 -11 -12 -13 -14 15 16 17 18 19 20
Sum of two lowest negative numbers of the said array of integers: -27
Original array elements:
-1 -2 -4 0 3 4 5 9
Sum of two lowest negative numbers of the said array of integers: -6

الأجوبة

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("\nSum of two lowest negative numbers of the said array of integers: " + test(nums));
            int[] nums1 = { -1, -2, -4, 0, 3, 4 , 5, 9 };
            Console.WriteLine("\nOriginal array elements:");
            for (int i = 0; i < nums1.Length; i++)
            {
                Console.Write(nums1[i] + " ");
            }
            Console.WriteLine("\nSum of two lowest negative numbers of the said array of integers: " + test(nums1));
        }
        public static int test(int[] values)
        {
            return values.Where(x => x < 0).OrderBy(x => x).Take(2).Sum();
        }
    }
}
هل كان المحتوى مفيد؟

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

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