Program to print the elements of an array

This is a simple program to create an array and then to print it’s all elements.

Now, just know about arrays.

Arrays are the special variables that store multiple values under the same name in the contiguous memory allocation. 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, 1.e., 0, 1, 2, 3 and 4.

Algorithm

  • STEP 1: START
  • STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}.
  • STEP 3: PRINT “Elements of given array:”
  • STEP 4: REPEAT STEP 5 for(i=0; i<arr.length; i++)
  • STEP 5: PRINT arr[i]
  • STEP 6: END

Program:

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

Output:

 Elements of given array:
1	2    3    4    5

Leave a Comment