C# program to check if all the values in a list that are greater than a given value

CsharpProgrammingServer Side Programming

Let’s say you need to find the elements in the following list to be greater than 80.

int[] arr = new int[] {55, 100, 87, 45};

For that, loop until the array length. Here, res = 80 i.e. the given element.

for (int i = 0; i < arr.Length; i++) {
   if(arr[i]<res) {
      Console.WriteLine(arr[i]);
   }
}

The following is the complete code −

Example

using System;
namespace Demo {
   public class Program {
      public static void Main(string[] args) {
         int[] arr = new int[] {
            55,
            100,
            87,
            45
         };
         // given integer
         int res = 80;
         Console.WriteLine("Given Integer {0}: ", res);
         Console.WriteLine("Numbers larger than {0} = ", res);
         for (int i = 0; i < arr.Length; i++) {
            if (arr[i] > res) {
               Console.WriteLine(arr[i]);
            }
         }
      }
   }
}
raja
Published on 21-Aug-2018 08:32:43
Advertisements