Java Program to Count Positive and Negative Numbers in an Array

Write a Java Program to Count Positive and Negative Numbers in an Array using For Loop, While Loop, and Functions with example.

Java Program to Count Positive and Negative Numbers in an Array using For Loop

This program allows the user to enter the size, and the One Dimensional Array elements. Next, it will count the total number of positive and negative numbers within this array using For Loop.

// Java Program to Count Positive and Negative Numbers in an Array
import java.util.Scanner;
public class CountPositiveNegative1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		int positiveCount = 0, negativeCount = 0;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}   

		for(i = 0; i < Size; i++)
		{
			if(a[i] >= 0)
			{
				positiveCount++;
			}
			else
			{
				negativeCount++;
			}
		}		
		System.out.println("\n Total Number of Positive Numbers in this Array = " + positiveCount);
		System.out.println("\n Total Number of Negative Numbers in this Array = " + negativeCount);
	}
}
Java Program to Count Positive and Negative Numbers in an Array 1

First, We declared One dimensional array of user specified size. Next, we used For Loop to store the user entered values as Array element such as a[0], a[1], a[2], a[3], a[4]

for (i = 0; i < Size; i++)
{
	a[i] = sc.nextInt();
}

In the next Java line, We have one more for loop to iterate each and every element. Within the For Loop, we used If statement.

Any number that is greater than or equal to 0 2 is a positive number. If statement will check the same.

  • If the condition is True then it is a Positive number, and the java compiler will increment positiveCount.
  • If the condition is False then it is a Negative number. The java compiler will increment negativeCount.
for(i = 0; i < Size; i++)
{
	if(a[i] >= 0)  {
		positiveCount++;
	}
	else  {
		negativeCount++;
	}
}

User inserted values are a[5] = {10, -15, 25, -19, -5}}

First Iteration: for (i = 0; 0 < 5; 0++)
The value of i will be 0 and the condition (i < 5) is True. So, it will start executing the If statement.

if(a[i] >= 0) => if(a[0] >= 0)
if(10 >= 0) – Condition is True. So, positiveCount will become 1

Second Iteration: for (i = 1; 1 < 5; 1++)
The condition (i < 5) is True.

if(a[1] >= 0)
if(-15 >= 0) – Condition is False. So, negativeCount will become 1

Do the same for remaining iterations until the condition (i < 5) fails.

Java Program to Count Positive and Negative Numbers in an Array using While Loop

This program is same as above but this time we used While Loop to count the positive and negative numbers in an array.

// Java Program to Count Positive and Negative Numbers in an Array using While Loop
import java.util.Scanner;
public class CountPositiveNegative2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i = 0, j = 0;
		int positiveCount = 0, negativeCount = 0;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		while (i < Size)
		{
			a[i] = sc.nextInt();
			i++;
		}   

		while (j < Size)
		{
			if(a[j] >= 0)
			{
				positiveCount++;
			}
			else
			{
				negativeCount++;
			}
			j++;
		}		
		System.out.println("\n Total Number of Positive Numbers in this Array = " + positiveCount);
		System.out.println("\n Total Number of Negative Numbers in this Array = " + negativeCount);
	}
}

Java Count Positive and Negative Array Items using a While Loop output

 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 4 -8 -12 15 -17 -25 105 110 -89 77

 Total Number of Positive Numbers in this Array = 5

 Total Number of Negative Numbers in this Array = 5

Java Program to Count Positive and Negative Numbers in an Array using Methods

This program is same as first example. But this time we created separate method to count positive numbers, and another method to count negative numbers.

// Java Program to Count Positive and Negative Numbers in an Array
import java.util.Scanner;
public class CountPositiveNegative3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		int positiveCount = 0, negativeCount = 0;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}   
		
		positiveCount = CountPositive(a, Size);
		negativeCount = CountNegative(a, Size);		
		System.out.println("\n Total Number of Positive Numbers in this Array = " + positiveCount);
		System.out.println(" Total Number of Negative Numbers in this Array = " + negativeCount);

	}
	public static int CountPositive(int [] a, int Size)
	{
		int i, positiveCount = 0;
		System.out.print("\n List of Positive Numbers in this Array are :");  
		for(i = 0; i < Size; i++)
		{
			if(a[i] >= 0)
			{
				System.out.print(a[i] +" ");
				positiveCount++;
			}
		}
		return positiveCount;
	}
	public static int CountNegative(int [] a, int Size)
	{
		int i, negativeCount = 0;
		System.out.print("\n List of Negative Numbers in this Array are :");  
		for(i = 0; i < Size; i++)
		{
			if(a[i] < 0)
			{
				System.out.print(a[i] + " ");
				negativeCount++;
			}
		}
		return negativeCount;
	}
}
 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 10 -25 -14 8 -19 -78 105 -77 -11 4

 List of Positive Numbers in this Array are :10 8 105 4 
 List of Negative Numbers in this Array are :-25 -14 -19 -78 -77 -11 
 Total Number of Positive Numbers in this Array = 4
 Total Number of Negative Numbers in this Array = 6