Q. Program to print the number of elements present in an array.

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.

Program to print the number of elements present in an array

Length of above array is 5. Hence, the number of elements present in the array is 5.

Algorithm

  1. Declare and initialize an array.
  2. Calculate the length of the array that is a number of elements present in the array.
  3. An in-built function can calculate length.
  4. Finally, print the length of the array.

Solution

Python

Arrays in Python is declared as

len() method returns the length of the array in Python.

Output:

Number of elements present in given array: 5

C

Arrays in C are declared as

"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:

Number of elements present in given array: 5

JAVA

Arrays in Java are created using a new keyword as

length property is used to calculate the length of the array.

Output:

Number of elements present in given array: 5

C#

Arrays in C# are created using new keyword as

Array. Length property is used to calculate the length of the array.

Output:

Number of elements present in given array: 5

PHP

Arrays in PHP are declared using Array ():

count () is in-built function in PHP to get the length of array.

Output:

Number of elements present in given array: 5

Next Topic#




Latest Courses