# Check if All Numbers in Array are Less than a Number in C++

This tutorial will discuss about a unique way to check if all numbers in array are less than a number in C++.

To check if all the elements of an array are less than a given number, we need to iterate over all the elements of array and check each element one by one.

For that we can use a STL Algorithm `std::all_of()`, which accepts the start & end iterators of an array as first two arguments. As this 3rd argument it will accept a Lambda function

This lambda function accept one argument and returns true, if the given argument is less than specific number, otherwise returns false.

The `std::all_of()` function will apply the given Lambda function on all the elements of array, and returns `true` only if the Lambda function returns `true` for all the elements of the array. The syntax is as follows,

```int arr[] = {8, 9, 6, 1, 2, 5, 10, 14};
int number = 20;

// Check if all numbers are less than a specific number
bool result = std::all_of(
std::begin(arr),
std::end(arr),
[&](const int& elem) {
return elem < number;
});
```

Here `std::all_of()` function function will return `true`, if all the numbers in the array are less than a specific number i.e. number 20.

Let’s see the complete example,

## Pointers in C/C++ [Full Course]

```#include <iostream>
#include <algorithm>

int main()
{
int arr[] = {8, 9, 6, 1, 2, 5, 10, 14};
int number = 20;

// Check if all numbers are less than a specific number
bool result = std::all_of(
std::begin(arr),
std::end(arr),
[&](const int& elem) {
return elem < number;
});

if(result)
{
std::cout << "Yes, all elements in the array are less than a specifc number \n";
}
else
{
std::cout << "No, all elements in the array are not less than a speciifc number" << std::endl;
}
return 0;
}
```

Output :

```Yes, all elements in the array are less than a specifc number
```

## Summary

Today we learned about a way to check if all numbers in array are less than a number in C++. Thanks.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top