Wednesday, 19 August 2020

Count Positive Negative Numbers in an Array

#Program to Count Positive and Negative Numbers in an Array

Step by step execution:

  1. Take Input as a array size and then array elements.
  2. Traverse the elements in the array one by one.
  3. For each element, check if the element is less than 0. If it is, then increment the count of negative elements.
  4. For each element, check if the element is greater than 0. If it is, then increment the count of positive elements.
  5. Print the count of negative and positive elements.
Code in JAVA:
import java.util.Scanner;
public class count 
{
public static void main(String[] args) 
{
int Positive_count=0,Negative_count=0;
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the Size of Array: ");
int size = scan.nextInt();
int [] number=new int[size];
System.out.println("Enter the elements of the Array: ");
for(int i=0;i<size;i++)
{
number[i]=scan.nextInt();
}
for(int i=0;i<size;i++)
{
if (number[i] >= 0)
{
Positive_count++;
}
else
{
Negative_count++;
}
}
System.out.println("The total number of Positive numbers in array are:- "+ Positive_count);
System.out.println("The total number of Negative numbers in array are:- "+ Negative_count);
}
}
Output:
Enter the Size of Array:9
Enter the elements of the Array: 12
-5
3
0
-9
-62
81
6
69
The total number of Positive numbers in array are:- 6
The total number of Negative numbers in array are:- 3

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32