codescracker


java

Java Program to Count Positive, Zero and Negative Numbers



« Previous Program Next Program »


Count Positive, Negative and Zero

To count the number of positive number, negative number, and zero from the given set of numbers entered by the user, you have to first ask to the user to enter a set of numbers (10 numbers here) to check all the number using for loop to count how many positive, negative, and zero present in the provided set of numbers and display the output on the screen as shown in the following program.

Java Programming Code to Count Positive, Zero and Negative

Following Java Program ask the user to enter 10 number to check for the occurrence of positive number, negative number and zero, then display the result on the screen:

/* Java Program Example - Count Positive, Negative, Zero */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int countp=0, countn=0, countz=0, i;
        int arr[] = new int[10];
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter 10 Numbers : ");
        for(i=0; i<10; i++)
        {
            arr[i] = scan.nextInt();
        }
        for(i=0; i<10; i++)
        {
            if(arr[i] < 0)
            {
                countn++;
            }
            else if(arr[i] == 0)
            {
                countz++;
            }
            else
            {
                countp++;
            }
        }
		
        System.out.print(countp + " Positive Numbers");
        System.out.print("\n" + countn + " Negative Numbers");
        System.out.print("\n" + countz + " Zero");
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program count positive negative zero

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test