find the range of all elements in an array in csharp

To find the range of all elements in an array in C#, you can use the Min and Max methods of the System.Linq namespace. These methods return the minimum and maximum values in a sequence, respectively. You can apply them to the array to find the range of all elements as shown below:

main.cs
using System.Linq;

int[] arr = { 10, 5, 2, 7, 15 };

int range = arr.Max() - arr.Min();

Console.WriteLine("Range of elements in the array: " + range);
153 chars
8 lines

Output:

main.cs
Range of elements in the array: 13
35 chars
2 lines

Here, we first import the System.Linq namespace and define an integer array arr with some sample values. We then calculate the range by subtracting the minimum value from the maximum value of the array, and store it in the range variable. Finally, we print the range to the console.

related categories

gistlibby LogSnag