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.
Suppose the original array is {2, 0, -1, 4, -6}
In the above array, there are 2 positive numbers, 2 negative numbers and 1 zero.
Suppose the original array is {-12, -23, -11, 64}
In the above array, there are 1 positive number and 3 negative numbers.
Suppose the original array is {11, 22, 0, 44, 0}
In the above array, there are 3 positive numbers and 2 zeros.
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.
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.
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); } }
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
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); } }
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, 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!