Open In App

C# Program to Check all Items of a Float Array is Greater than 5.0 using LINQ

Improve
Improve
Like Article
Like
Save
Share
Report

Given a float array, now our task is to check if all the elements present in the float array are greater than 5.0. So we use the All() method of LINQ. This method is used to check if all the elements in the given sequence satisfy the given condition. It will return true if all the elements of the specified sequence pass the test in the given predicate, otherwise, it will return false. So to solve the given problem we use the following query:

result = nums.All(element => element > 5.0F);

Here, the result is a boolean type variable that stores the final result, nums is the array of float, and the All() method contains the condition which checks each element present in the given array satisfy the condition or not. Here if all the elements of the array fulfill the given condition then this statement “nums.All(element => element > 5.0F)” will return true. If any of the elements does not fulfill the given condition then it will return false.

Example: 

Input  : [6.0, 7.89, 8.9, 89.5, 13.3]
Output : true
 
Input  : [2.0, 1.89, 2.9, 89.5, 13.3]
Output : false

Example 1:

C#




// C# program to check if all the items of a
// Float Array is greater than 5.0
using System;
using System.Linq;
 
class geeks{
     
static void Main(string[] args)
{
     
    // Creating a float array
    float[] nums = { 1.2f, 30.3f, 5.6f, 7.0f, 10.1f };
     
    bool result;
     
    // Checking the given array contains all the
    // elements that are greater than 5.0
    result = nums.All(element => element > 5.0F);
     
    if (result == true)
    {
        Console.Write("Elements in the array are greater than 5");
    }
    else
    {
        Console.Write("Elements in the array are not greater than 5");
    }
}
}


Output:

Elements in the array are not greater than 5

Example 2:

C#




// C# program to check if all the items of a
// Float Array is greater than 5.0
using System;
using System.Linq;
 
class geeks{
     
static void Main(string[] args)
{
     
    // Creating a float array
    float[] nums1 = { 1.2f, 30.3f, 3.6f, 7.0f, 10.1f };
    float[] nums2 = { 6.2f, 10.3f, 9.6f, 19.0f, 20.1f };
     
    bool result1, result2;
     
    // Checking the given array contains all the
    // elements that are greater than 5.0
    result1 = nums1.All(element => element > 5.0F);
    result2 = nums2.All(element => element > 5.0F);
     
    // Displaying the final result
    Console.WriteLine("Is nums1 contain all the elements " +
                      "greater than 5.0f: " + result1);
    Console.WriteLine("Is nums2 contain all the elements " +
                      "greater than 5.0f: " + result2);
}
}


Output:

Is nums1 contain all the elements greater than 5.0f: False
Is nums2 contain all the elements greater than 5.0f: True


Last Updated : 09 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads