# Find count of positive and negative array elements in Java

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

As per the problem statement we have to find count of positive numbers, negative numbers and zero present in the given array.

Any number which is greater than zero that is called as positive number, if the number is less than zero then that is negative number else the given number is a zero.

Let’s see how we can do it by using Java programming language.

### To Show You Some Instances

#### Instance-1

Suppose the original array is {2, 0, -1, 4, -6}


In the above array, 2 positive numbers, 2 negative numbers and 1 zero are present.

#### Instance-2

Suppose the original array is {-12, -23, -11, 64}


In the above array, 1 positive number and 3 negative numbers are present.

#### Instance-3

Suppose the original array is {11, 22, 0, 44, 0}


In the above array, 3 positive numbers and 2 zeros are present.

### Algorithm

• Step 1 − Declare and initialize an integer array. Take 3 variables to keep count of positive, negative and zero elements.

• Step 2 − Iterate each element of the array and check it is greater than zero or less than zero or zero. Respectively increase the count value.

• Step 3 − Finally print the result.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Array Elements

• 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 Elements

### Example

In this approach, array elements will be initialized in the program. Then as per the algorithm check the total count of positive, negative, and zero elements.

import java.util.Arrays;
public class Main{

//main method
public static void main(String args[]){

//declared 3 integer variables and initialized all with zero
int positiveCount, negativeCount, zeroCount;
positiveCount=negativeCount=zeroCount=0;

//Declare and initialize the array elements
int arr[] = {4, 8, -2, 3, -1, 0, 7, 0, -9};

//get the length of the array
int size=arr.length;

// Print the array elements
System.out.println("Array elements are: "+Arrays.toString(arr));

//iterate each element of array
for(int i=0; i < arr.length; i++) {

//check positive number
if(arr[i] > 0)
positiveCount++;

//check negative number
else if(arr[i] < 0)
negativeCount++;

//check zero
else
zeroCount++;
}

//print the result
System.out.println("Count of positive numbers in array: "+positiveCount);
System.out.println("Count of negative numbers in array: "+negativeCount);
System.out.println("Count of zeroes in array: "+zeroCount);
}
}


### Output

Array elements are: [4, 8, -2, 3, -1, 0, 7, 0, -9]
Count of positive numbers in array: 4
Count of negative numbers in array: 3
Count of zeroes in array: 2


## 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 a parameter and inside the method as per the algorithm check the total count of positive, negative, and zero elements.

import java.util.Arrays;
public class Main{

//main method
public static void main(String args[]){

//Declare and initialize the array elements
int arr[] = {4, -2, 3, 7, 0, -9};

//calling the user defined method
findCount(arr);
}

//method to find frequency of postive, negative and zero elements
public static void findCount(int []arr){

//declared 3 integer variables and initialized all with zero
int positiveCount, negativeCount, zeroCount;
positiveCount=negativeCount=zeroCount=0;

//get the length of the array
int size=arr.length;

// Print the array elements
System.out.println("Array elements are: "+Arrays.toString(arr));

//iterate each element of array
for(int i=0; i < arr.length; i++) {

//check positive number
if(arr[i] > 0)
positiveCount++;

//check negative number
else if(arr[i] < 0)
negativeCount++;

//check zero
else
zeroCount++;
}

//print the result
System.out.println("Count of positive numbers in array: "+positiveCount);
System.out.println("Count of negative numbers in array: "+negativeCount);
System.out.println("Count of zeroes in array: "+zeroCount);
}
}


### Output

Array elements are: [4, -2, 3, 7, 0, -9]
Count of positive numbers in array: 3
Count of negative numbers in array: 2
Count of zeroes in array: 1


In this article, we explored how to find the frequency of positive numbers, negative numbers and zeroes in an array in Java.

Updated on: 05-Jan-2023

4K+ Views