Q:

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

belongs to collection: Java Array Programs

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to access elements of character array... >>
<< Java program to find smallest element in an array...