java

Program to count Positive, Negative and zero in an array of Java

Program to count Positive, Negative and zero in an array of Java

In this article, we will discuss the concept of program to count Positive, Negative and zero in an array of Java

In this code, we are going to learn how to count Positive numbers, Negative numbers and zeros in an array using several ways in Java language.

This is done using for loop , while loop , do-while loop and method in Java language

Program to count Positive, Negative and zero in an array of Java

Count Positive, Negative and zero in an array using for loop

In this program, we will count positive numbers ,negative numbers and zeros from the given number in an array  using a for loop in Java language

Program 1

import java.util.Scanner;
public class Count_Pos_NegArray{
public static void main(String args[]){
  int i,num,size,posCount=0,negCount=0,zeroCount=0;
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Pease Enter number of elements of an array: ");
size=sc.nextInt();
int []arr=new int[size];
System.out.println("pease enter "+size+" eements of an array");

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

for(i=0; i<size; i++){
 if(arr[i]>0){
   posCount++;
 }
  else if(arr[i]<0){
   negCount++;
 }
 else{
   zeroCount++;
 }
}

System.out.println("Negative Numbers are: "+negCount);
System.out.println("Positive Numbers are: "+posCount);
System.out.println("Zeros are: "+zeroCount);
}
}

When the above code is executed, it produces the following result

Count Positive, Negative and zero in an array

Count Positive, Negative and zero in an array using while loop

In this program, we will count positive numbers ,negative numbers and zeros from the given number in an array using a while loop in Java language

Program 2

import java.util.Scanner;
public class Count_Pos_NegArray1{
public static void main(String args[]){
  int i,num,size,posCount=0,negCount=0,zeroCount=0;
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Pease Enter number of elements of an array: ");
size=sc.nextInt();
int []arr=new int[size];
System.out.println("pease enter "+size+" eements of an array");

i=0; 
while(i<size){
 arr[i]=sc.nextInt();
 i++;
}

i=0; 
while(i<size){
 if(arr[i]>0){
   posCount++;
 }
  else if(arr[i]<0){
   negCount++;
 }
 else{
   zeroCount++;
 }
  i++;
}

System.out.println("Negative Numbers are: "+negCount);
System.out.println("Positive Numbers are: "+posCount);
System.out.println("Zeros are: "+zeroCount);
}
}

When the above code is executed, it produces the following result

Output

 

Count Positive, Negative and zero in an array using do-while loop

In this program, we will count positive numbers ,negative numbers and zeros from the given number in an array using a do-while loop in Java language

Program 3

import java.util.Scanner;
public class Count_Pos_NegArray2{
public static void main(String args[]){
  int i,num,size,posCount=0,negCount=0,zeroCount=0;
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Pease Enter number of elements of an array: ");
size=sc.nextInt();
int []arr=new int[size];
System.out.println("pease enter "+size+" eements of an array");

i=0; 
do{
 arr[i]=sc.nextInt();
 i++;
}while(i<size);

i=0; 
do{
 if(arr[i]>0){
   posCount++;
 }
  else if(arr[i]<0){
   negCount++;
 }
 else{
   zeroCount++;
 }
  i++;
}while(i<size);

System.out.println("Negative Numbers are: "+negCount);
System.out.println("Positive Numbers are: "+posCount);
System.out.println("Zeros are: "+zeroCount);
}
}

When the above code is executed, it produces the following result

Output

Count Positive, Negative and zero in an array using method in Java

In this program, we will count positive numbers ,negative numbers and zero from the given number in an array  using a method in Java language

Program 4

import java.util.Scanner;
public class Count_Pos_NegArrayMethod{
public static void main(String args[]){
  int i,num,size,posCount=0,negCount=0,zeroCount=0;
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Please Enter number of elements of an array: ");
size=sc.nextInt();
int []arr=new int[size];
System.out.println("please enter "+size+" elements of an array");

for(i=0; i<size; i++){
 arr[i]=sc.nextInt();
}
System.out.println("\nTotal number of positive in this array:"+countPositive(arr,size));
//call the method for positve count
System.out.println("\nTotal number of negative in this array:"+countNegative(arr,size));
//call the method for negative count
System.out.println("\nTotal number of zero in this array:"+countZero(arr,size));
//call the method for zero count

}


public static int countPositive(int []arr,int size){//method for positive count
  int i,posCount=0;
  System.out.println("The positive numbers in the array are:");
  for(i=0; i<size; i++){
    if(arr[i]>0){
      System.out.print(arr[i]+" ");
      posCount++;
    }
    
  }return posCount;
  
}
public static int countNegative(int []arr,int size){//method for negative count
  int i,negCount=0;
  System.out.println("The negative numbers in the array are:");
  for(i=0; i<size; i++){
    if(arr[i]<0){
      System.out.print(arr[i]+" ");
      negCount++;
    }
    
  }return negCount;
  
}

public static int countZero(int []arr,int size){//method for zero count
  int i,zeroCount=0;
  System.out.println("Zeros in the array are:");
  for(i=0; i<size; i++){
    if(arr[i]==0){
      System.out.print(arr[i]+" ");
      zeroCount++;
    }
    
  }return zeroCount;
  
}
}

When the above code is executed, it produces the following result

Output

 

Suggested post

Operator in Java

For loop in Java

while loop in java

Do-while loop in java

Data type in Java

Method in Java

 

Similar post

Code to count Positive, Negative and zero in an array of C++

Code to count Positive, Negative and zero in an array of Python

Code to count Positive, Negative and zero in an array of C

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.