Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Code to count Positive, Negative and zero in an array of C

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

Posted on November 4, 2020November 8, 2020

Table of Contents

  • Program to count Positive, Negative and zero in an array of Java
    • Program to count Positive, Negative and zero in an array of Java
      • Count Positive, Negative and zero in an array using for loop
      • Count Positive, Negative and zero in an array using while loop
      • Count Positive, Negative and zero in an array using do-while loop
      • Count Positive, Negative and zero in an array using method in 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

Program to count Positive, Negative and zero in an array of Java
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

Program to count Positive, Negative and zero in an array of Java
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

Program to count Positive, Negative and zero in an array of Java
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

Program to count Positive, Negative and zero in an array of Java
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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes