Count Positive Negative and Zero in Java Array

Program description:- Write a Java program to take an array and count positive, negative, and zero in the given array. Display the result.

Example:-
Array = {-15, -10, -5, 0, 5, 10, 15}
Positive numbers in the array = 3
Negative numbers in the array = 3
Zero in the array = 1

Positive, negative, and zero:- If a number is greater than zero (>0) then it is a positive number, else if the number is less than zero (<0) then it is a negative number, else it is zero.

Prerequisite:- Array in Java, find the length of an array in Java

Procedure to develop a Java method to count the positive negative and zero in the given array,

a) Take an array.
b) Take three variables to store the count value of positive, negative, and zeros. Assume the variables are:- positive, negative, and zero.
c) Traverse the array. To traverse we will use the for-each loop.
d) Check each element of the array.
e) If it is greater than zero (>0) then it is a positive number therefore increase the count of a positive number by 1.
f) Else if it is less than zero (<0) then it is a negative number therefore increase the count of the negative number by 1.
g) Else it is zero and increases the count of a negative number by 1.
h) Display the resultant values.

In this program, we will take input value for the array from the end-user, but you can take it explicitly. See:- how to take array input in Java. We will use a method to perform the operation. 

Java Program to Count Positive Negative and Zero in Array

import java.util.Scanner;

public class ArrayTest {

  public static void main(String[] args) {
    // create Scanner class object to take input
    Scanner scan = new Scanner(System.in);

    // read size of the array
    System.out.print("Enter size of the array: ");
    int n = scan.nextInt();

    // create an int array of size n
    int numbers[] = new int[n];

    // take input for the array
    System.out.println("Enter array elements: ");
    for (int i = 0; i < n; ++i) {
      numbers[i] = scan.nextInt();
    }

    // find of +ve, -ve, and zero
    checkNumbers(numbers);

    // close Scanner
    scan.close();

  }

  // method to count +ve, -ve, zero 
  public static void checkNumbers(int[] numbers) {

    // variables
    int positive = 0;
    int negative = 0;
    int zero = 0;

    // check each element
    for (int num : numbers) {
      if (num > 0) {        // +ve
        ++positive;
      } else if (num < 0) { // -ve
        ++negative;
      } else {              // 0
        ++zero;
      }
    }

    // display resultant values
    System.out.println("Positive numbers = " + positive);
    System.out.println("Negative numbers = " + negative);
    System.out.println("Zeros = " + zero);
  }
}

Output:-

Enter size of the array: 7
Enter array elements:
-15 -10 -5 0 5 10 15
Positive numbers = 3
Negative numbers = 3
Zeros = 1

Enter size of the array: 7
Enter array elements:
9 8 7 0 -2 0 5
Positive numbers = 4
Negative numbers = 1
Zeros = 2

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *