TheDeveloperBlog.com

Home | Contact Us

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

Program to print the elements of an array

Program to print the elements of 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 elements of an array.

Explanation

In this program, we need to create an array and print the elements present in the array.

Arrays are the special variable that stores multiple values under the same name. A contiguous memory will be allocated to store elements. Elements of the array can be accessed through their indexes.

Program to print the elements of an array

Here, 1, 2, 3, 4 and 5 represent the elements of the array. These elements can be accessed through their corresponding indexes, i.e., 0, 1, 2, 3 and 4.

Algorithm

  1. Declare and initialize an array.
  2. Loop through the array by incrementing the value of i.
  3. Finally, print out each element of the array.

Solution

Python

#Initialize array 
arr = [1, 2, 3, 4, 5]; 
 
print("Elements of given array: ");
#Loop through the array by incrementing the value of i 

for i in range(0, len(arr)):
    print(arr[i]), 

Output:

Elements of given array: 
1 2 3 4 5

C

#include <stdio.h>
 
int main()
{
    //Initialize array 
    int arr[] = {1, 2, 3, 4, 5}; 
    //Calculate length of array
    int length = sizeof(arr)/sizeof(arr[0]);
    
    printf("Elements of given array: \n");
    //Loop through the array by incrementing value of i 
    for (int i = 0; i < length; i++) { 
        printf("%d ", arr[i]); 
    }  
 
    return 0;
}

Output:

Elements of given array: 
1 2 3 4 5

JAVA

public class PrintArray {
    public static void main(String[] args) {    
        
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        
        System.out.println("Elements of given array: ");
        //Loop through the array by incrementing value of i 
        for (int i = 0; i < arr.length; i++) { 
            System.out.print(arr[i] + " "); 
        }  
    }
}

Output:

Elements of given array: 
1 2 3 4 5 

C#

 using System;
                    
public class PrintArray
{
    public static void Main()
    {
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        
        Console.WriteLine("Elements of given array: ");
        //Loop through the array by incrementing value of i 
        for (int i = 0; i < arr.Length; i++) { 
            Console.Write(arr[i] + " "); 
        }  
    }
}

Output:

Elements of given array: 
1 2 3 4 5

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array 
$arr = array(1, 2, 3, 4, 5); 
 
print("Elements of given array: <br>");
//Loop through the array by incrementing value of i 
for ($i = 0; $i < count($arr); $i++) { 
    print($arr[$i] . " "); 
}  
?>
</body>
</html>

Output:

Elements of given array: 
1 2 3 4 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