TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print the number of elements present in an array

Program to print the number of elements present in an array on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

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

ArrayName = [ele1, ele2,...];

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

#Initialize array 
arr = [1, 2, 3, 4, 5]; 
 
#Number of elements present in an array can be found using len()
print("Number of elements present in given array: " + str(len(arr))); 

Output:

Number of elements present in given array: 5

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.

#include <stdio.h>
 
int main()
{
    //Initialize array 
    int arr[] = {1, 2, 3, 4, 5}; 
    
    //Number of elements present in an array can be calculated as follows
    int length = sizeof(arr)/sizeof(arr[0]);
    
    printf("Number of elements present in given array: %d", length); 
 
    return 0;
}

Output:

Number of elements present in given array: 5

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.

public class CountArray {
    public static void main(String[] args) {
        
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        
        //Number of elements present in an array can be found using the length

        System.out.println("Number of elements present in given array: " + arr.length);   
    }
}

Output:

Number of elements present in given array: 5

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.

using System;
                    
public class CountArray
{
    public static void Main()
    {
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        
        //Number of elements present in an array can be found using Length
        Console.WriteLine("Number of elements present in given array: " + arr.Length); 
    }
}

Output:

Number of elements present in given array: 5

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.

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array 
$arr = array(1, 2, 3, 4, 5); 
 
//Number of elements present in an array can be found using count()
print("Number of elements present in given array: " . count($arr));
?>
</body>
</html>

Output:

Number of elements present in given array: 5

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf