Check if Array is Free From 0 and Negative Number in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to check and confirm the array is free from 0 and negative numbers. Let us say that an array contains 0 or a negative element then the result should pop up that "Array contains 0 or a negative number." Otherwise, the result should be "Array doesn’t contain 0 or a negative number."

Note − The array must be an integer array.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {4, 0, 3, 1, 8, 5, 9}

After checking for 0 or negative element in an array the result will be −

Array contain 0 or a negative number.

Instance-2

Suppose the original array is {4, 3, -6, 1, 8, 11, 9}

After checking for 0 or negative element in an array the result will be −

Array contain 0 or a negative number.

Instance-3

Suppose the original array is {4, 24, 3, 1, 18, 51, 9}

After checking for 0 or negative element in an array the result will be −

Array do not contain 0 or a negative number.

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Set boolean flag = false;

  • Step 3 − Check if there are any negative numbers or 0 using the condition “if(arr[i] <= 0)”.

  • Step 4 − If step 3 is true then turn “flag = true”

  • Step 5 − Print Array contains 0 or a negative number.

  • Step 6 − Else Print Array does not contain 0 or a negative number.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

Where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm check and confirm the array is free from 0 and negative numbers.

public class Main{
   //main method
   public static void main(String[] args) {
   
      //Declare and initialize the array elements
      int arr[] = { 4, 3, -6, 1, 8, 11, 9 };
      
      //declare boolean to check for negative or 0
      boolean flag = false;
      
      // check is there any negative numbers or 0
      for (int i = 0; i < arr.length; i++) {
         if(arr[i] <= 0){
            flag = true;
            break;
         }
      }
      
      // -ve or 0 number is not available
      if(!flag) {
         System.out.println("Array does not contain 0 or a negative number.");
         return;
      }
      
      // -ve or 0 number is available
      else
         System.out.println("Array contain 0 or a negative number.");   
   }
}

Output

Array contain 0 or a negative number.

Approach-2: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm check and confirm the array is free from 0 and negative number.

public class Main{
    //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int arr[] = { 4, 24, 3, 1, 18, 51, 9 };
      
      //calling a user defined method
      check(arr);
   }
   
   //user defined method
   public static void check(int []arr){
   
      //declare boolean to check for negative or 0
      boolean flag = false;
      
      // check is there any negative numbers or 0
      for (int i = 0; i < arr.length; i++) {
         if(arr[i] <= 0){
            flag = true;
            break;
         }
      }
      
      // -ve or 0 number is not available
      if(!flag) {
         System.out.println("Array do not contain 0 or a negative number.");
         return;
      }
      
      // -ve or 0 number is available
      else
         System.out.println("Array contain 0 or a negative number.");   
   }
}

Output

Array do not contain 0 or a negative number.

In this article, we explored how to check and confirm an array is free from 0 and negative number in Java.

Updated on: 05-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements