Print Array using loop

3
1
2

is expected result after the execution of the code.


C

#include<stdio.h>
int main() {
    int array[] = {3, 1, 2};
    for(unsigned int i = 0;i < sizeof(array)/sizeof(array[0]); i++) {
        printf("%i\n", array[i]);
    }
}

C++

 #include<iostrem>
int main() {
    int array[] = {3, 1, 2};
    for(unsigned int i = 0;i < sizeof(array)/sizeof(array[0]); i++) {
        cout << array[i] << endl;
    }
}

C#

using System;
public class ArrayPrinter {
    public void printArray() {
        int[] array = {3, 1, 2};
        foreach (int i in array)
        {
            System.Console.Write("{0}\n", i);
        }
    }
    public static void Main()  {
         ArrayPrinter ap = new ArrayPrinter();
         ap.printArray();
    }
}

Go

package main
import "fmt"
func main() {
	array := [3]int{3, 1, 2}
	for _, num := range array {
		fmt.Println(num)
	}
}

Java

public class ArrayPrinter {
    public static void main(String[] args) {
        int[] myList = {3, 1, 2};
        for(int i = 0;i<myList.length;i++) {
            System.out.println(myList[i]);
        }
    }
}

JavaScript

let array = [3, 1, 2]
for(const i in array) {
    console.log(array[i]);
}

Perl

my @array = (3, 1, 2);
foreach $elem (@array) {
    print $elem."\n";
}

PHP

<?php
$array = [3, 1, 2];
foreach ($array as $elem) {
    print $elem."\n";
}

Python

array = [3, 1, 2]
for elem in array:
    print(str(elem))

Ruby

array = [3, 1, 2]
for elem in array
    print(elem.to_s+"\n")
end

Shell

#!/bin/sh
array=(3 1 2)
for i in ${array[@]}
do
    echo ${i};
done