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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes