Home > Java > javaTutorial > body text

Find number of positive and negative array elements in Java

WBOY
Release: 2023-08-29 14:25:05
forward
1147 people have browsed it

Find number of positive and negative array elements in Java

In Java, an array is a non-primitive data type that stores values ​​of similar data types.

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

Any number greater than zero is called a positive number, if the number is less than zero, it is negative, otherwise it is zero.

Let us see how to implement it using Java programming language.

Show you some examples

Example 1

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

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

Example 2

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

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

Example 3

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

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

algorithm

  • Step 1 - Declare and initialize an integer array. Use 3 variables to record the number of positive, negative and zero elements.

  • Step 2 - Iterate over each element of the array and check if it is greater than zero, less than zero or equal to zero. Increment the count value respectively.

  • Step 3 - Finally print the result.

Multiple methods

We provide solutions in different ways.

  • By using static initialization of array elements

  • By using user-defined methods

Let's look at the program and its output one by one.

Method 1: By using static initialization of array elements

Example

In this method, the array elements will be initialized in the program. The total number of positive, negative and zero elements is then checked according to the algorithm.

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);
   }
}
Copy after login

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
Copy after login

Method 2: Using user-defined methods

Example

In this method, the array elements will be initialized in the program. A user-defined method is then called by passing the array as argument and within the method the total number of positive, negative and zero elements is checked according to the algorithm.

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);
   }
}
Copy after login

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
Copy after login

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

The above is the detailed content of Find number of positive and negative array elements in Java. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template