C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q. Program to print the elements of an array.ExplanationIn 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
SolutionPython#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 JAVApublic 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#
|