How to count array elements with values in a given range with C#

1 Answer

0 votes
using System;

class Program
{
    public static int countValuesInRange(int []arr, int from, int to) {
        int count = 0;
        int size = arr.Length;
        for (int i = 0; i < size; i++) {
            if (arr[i] >= from && arr[i] <= to)
                count++;
        }
        return count;
    }
    static void Main() {
        int []arr = { 3, 6, 0, 17, 9, 1, 8, 7, 4, 10, 18, 11, 20, 30 };
        int from = 5, to = 12;
      
        Console.Write(countValuesInRange(arr, from, to));
    }
}


    
    
       
/*
run:
       
6
       
*/

 



answered Dec 16, 2021 by avibootz

Related questions

...