Java Program to print the number of elements present in an array

In this program, we need to count and print the number of elements present in the array.

The number of elements present in the array can be found by calculating the length of the array.

Java Program to print the number of elements present in an array

Length of above array is 5. Hence, the number of elements present in the array is 5.

Algorithm

  • STEP 1: START
  • STEP 2: INITIALIZE arr = {1,2,3,4,5}
  • STEP 3: PRINT arr.length
  • STEP 4: EXIT

Program:

  1. public class CountArray {  
  2.     public static void main(String[] args) {  
  3.       //Initialize array  
  4.         int [] arr = new int [] {1, 2, 3, 4, 5};  
  5.       //Number of elements present in an array can be found using the length  
  6.         System.out.println(“Number of elements present in given array: ” + arr.length);  
  7.     }  
  8. }  

Output:

Number of elements present in given array: 5

Leave a Comment