Java program to count total positives, negatives and zeros from an array

In this java program, we are going to count total number of negative, positive and zero elements in a given array of integers. By IncludeHelp Last updated : December 23, 2023

Problem statement

Given an array of integers and we have to count total negatives, positives and zeros using java program.

Example

Input:
Array elements: 20, -10, 15, 00, -85

Output:
Positive Numbers are: 2
Negative Numbers are: 2
Zeros are: 1

Program to count positive, negative and zeros from an array in java

import java.util.Scanner;

public class CalcNumbersType
{
    public static void main(String args[])
    {
    	// intialize and declaring the objects.
        int n,positive=0, negative=0, zero=0, i;
        int arr[] = new int[50];
        Scanner scan = new Scanner(System.in);
       
        // enter number you have to enter.
        System.out.print("How many Number you want to Enter : ");
        n = scan.nextInt();
		
        // enter the numbers.
        System.out.println("Enter " +n+ " Numbers : ");
      
        // this is to calculate the type of the number.
        for(i=0; i<n; i++)
        {
            arr[i] = scan.nextInt();
        }
        for(i=0; i<n; i++)
        {
            if(arr[i] < 0)
            {
                negative++;
            }
            else if(arr[i] == 0)
            {
                zero++;
            }
            else
            {
                positive++;
            }
        }
		// print all +ve,-ve and zero number.
        System.out.print("Positive Numbers are: " + positive );
        System.out.print("\nNegative Numbers are: " + negative );
        System.out.print("\nZeros are: " + zero );
    }
}

Output

How many Number you want to Enter : 5
Enter 5 Numbers : 
20
-10
15
00
-85
Positive Numbers are: 2
Negative Numbers are: 2
Zeros are: 1

Java Array Programs »

More Java Array Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.