Q:

Program to print the elements of an array

belongs to collection: Array Programs

0

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.

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.

Input:

arr = [1, 2, 3, 4, 5]  

Output:

Elements of given array: 1 2 3 4 5

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to print the elements of an array in rever... >>
<< Program to print the duplicate elements of an arra...