Q:

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

belongs to collection: Java Array Programs

0

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.

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

All Answers

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

Program:

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

Output:

Number of elements present in given array: 5

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

total answers (1)

Java Program to print the sum of all the items of ... >>
<< Program to print the smallest element in an array...