In the previous article, we have seen Java Program to Remove Even Numbers from Array
In this article we are going to see how we can count the number of negative, positive and zero elements by using Java programming language.
Java Program to Count Positive Negative and Zero from Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to count positive, negative and zero element in an array.
Method-1: Java Program to Count Positive Negative and Zero from Array By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Iterate the array.
- If array element is greater than zero then it is positive number so increase the positive count.
- If array element is smaller than zero then it is negative number so increase the negative count.
- Else element is equal to zero then it is zero so increase the zero count.
- Then print the result.
Program:
public class Main { public static void main(String args[]) { // Array with elements int arr[] = {10,-15,1,-30,50,7,1,0,0}; //variable to store count of positive, negative and zero elements //initialized with to 0 int positive=0,negative=0,zero=0; //Loop to count positive,negative and zero elements for(int row=0;row<arr.length;row++) { //if array element is greater than zero it is positive if(arr[row]>0) //incrementing positive count value positive++; //if array element is smaller than zero it is negative else if(arr[row]<0) //incrementing negative count value negative++; //if array element is not greater or smaller than zero then it is equal to zero else //incrementing zero count value zero++; } System.out.println("Number of positive elements are : "+positive); System.out.println("Number of negative elements are : "+negative); System.out.println("Number of zero elements are : "+zero); } }
Output: Number of positive elements are : 5 Number of negative elements are : 2 Number of zero elements are : 2
Method-2: Java Program to Count Positive Negative and Zero from Array By Dynamic Initialization of Array Elements
Approach:
- Take the array size input from user.
- Take the input of array elements.
- Print the array.
- Then Iterate the array.
- If array element is greater than zero then it is positive number so increase the positive count.
- If array element is smaller than zero then it is negative number so increase the negative count.
- Else element is equal to zero then it is zero so increase the zero count.
- Then print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Array with elements int arr[] = null; System.out.print("Enter the length of the array : "); int length = scan.nextInt(); arr = new int[length]; int iter; // Entering the array elements System.out.println("Enter the array elements : "); for(iter=0;iter<arr.length;iter++) arr[iter]=scan.nextInt(); System.out.println("The array elements are : "); //For Loop to print the elements for(iter=0;iter<arr.length;iter++) { System.out.print(arr[iter]+" "); } //variable to store count of positive,negative and zero elements //initialized with to 0 int positive=0,negative=0,zero=0; //Loop to count positive,negative and zero elements for(int row=0;row<arr.length;row++) { //if array element is greater than zero it is positive if(arr[row]>0) //incrementing positive count value positive++; //if array element is smaller than zero it is negative else if(arr[row]<0) //incrementing negative count value negative++; //if array element is not greater or smaller than zero then it is equal to zero else //incrementing zero count value zero++; } System.out.println("\nNumber of positive elements are : "+positive); System.out.println("Number of negative elements are : "+negative); System.out.println("Number of zero elements are : "+zero); } }
Output: Enter the length of the array : 10 Enter the array elements : 1 -2 3 -4 5 0 6 0 -7 8 The array elements are : 1 -2 3 -4 5 0 6 0 -7 8 Number of positive elements are : 5 Number of negative elements are : 3 Number of zero elements are : 2
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs:
- Java Program to Separate Positive Negative and Zero elements from Array and Store in Separate arrays
- Java Program to Find the Elements from an Array which are Greater than a Given Number
- Java Program to Find the Elements from an Array which are Smaller than a Given Number
- Java Program to Find the Elements from the Array which are Greater than the Average of Array