Write a C# Sharp program to re-arrange the elements in a given array of numbers and check the numbers are consecutive or not
- برمجة سي شارب
- برمجة
- 2021-05-29
- 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("\nCheck the numbers of the said array are consecutive or not! " + test(nums));
int[] nums1 = { -1, -2, -3, 0, 1, 4, 3, 2 };
Console.WriteLine("\nOriginal array elements:");
for (int i = 0; i < nums1.Length; i++)
{
Console.Write(nums1[i] + " ");
}
Console.WriteLine("\nCheck the numbers of the said array are consecutive or not! " + test(nums1));
}
public static bool test(int[] nums)
{
Array.Sort(nums);
for (int i = 1; i < nums.Length; i++)
if (nums[i] - nums[i - 1] != 1)
return false;
return true;
}
}
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال