The problem we are going to discuss today is “How to get the number of elements in an array in C++”. First of all we will discuss the approach or the algorithm and then we will write a program to do so.

# Approach

## Using size operator

We know that sizeof() is used to find the length. To find the number of elements using size operator we will follow this method. If we divide the array filled with elements by the array containing one element then we will get the number of elements of the filled array. We will be learning how to use that to find the length of the array.

In this program, the length is calculated by finding the size of the array and dividing it by size of one element.

```
#include <iostream>
using namespace std;
int main() {
int arr[5] = {4, 1, 8, 2, 9};
int len = sizeof(arr)/sizeof(arr[0]);
cout << "The length of the array is: " << len;
return 0;
}
```

## Using pointers

We can use pointers to find the length of the array. The pointer contains the address of the elements in the array. The value in the array is the address of the starting element and the last element in the array. So their subtraction gives the result. So this is the method of getting the length of the array.

```
#include <iostream>
using namespace std;
int main() {
int arr[5] = {5, 8, 1, 3, 6};
int len = *(&arr + 1) - arr;
cout << "The length of the array is: " << len;
return 0;
}
```

If you have any kind of doubt then feel free to ask in the comment section.