Explanation
In this program, we need to count and print the number of elements present in the array.
A number of elements present in the array can be found by calculating the length of the array.
Length of above array is 5. Hence, the number of elements present in the array is 5.
Algorithm
- Declare and initialize an array.
- Calculate the length of the array that is a number of elements present in the array.
- An in-built function can calculate length.
- Finally, print the length of the array.
Input:
arr = [1, 2, 3, 4, 5]
Output:
Number of elements present in given array: 5
Python
Arrays in Python is declared as
ArrayName = [ele1, ele2,...];
len() method returns the length of the array in Python.
Output:
C
Arrays in C are declared as
datatype arrayName [size] = {element1, element2,..};
"sizeof" operator gives the size of the array in bytes so, to get the length of the array we divide the size of the array with the size of the first element.
Output:
JAVA
Arrays in Java are created using a new keyword as
datatype [] arrayName = new datatype[] {ele1, ele2,...};
length property is used to calculate the length of the array.
Output:
C#
Arrays in C# are created using new keyword as
datatype [] arrayName = new datatype[] {ele1, ele2,...};
Array. Length property is used to calculate the length of the array.
Output:
PHP
Arrays in PHP are declared using Array ():
$arrayName = Array (element1, element2,..);
count () is in-built function in PHP to get the length of array.
Output: