Table of Contents
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 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
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
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
Suggested post
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