Write a class CountArray to accept the values and count the total number of negative and positive numbers and display it separately

0

Assignment: Module 03- Part 03

Write a class CountArray to accept the values and count the total number of negative and positive numbers and display it separately
Assignment: Module 03- Part 03

Write a class CountArray to accept the values and count the total number of negative and positive numbers and display it separately

Input: 

Enter the array size 

4

23

-3

 -2

21

Output: 

2

2

import java.util.Scanner;

public class CountArray {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the array size: ");
        int size = sc.nextInt();
        int[] array = new int[size];
        
        System.out.println("Enter the array values: ");
        for (int i = 0; i < size; i++) {
            array[i] = sc.nextInt();
        }
        
        int positive = 0;
        int negative = 0;
        for (int i = 0; i < size; i++) {
            if (array[i] >= 0) {
                positive++;
            } else {
                negative++;
            }
        }
        
        System.out.println("Number of positive numbers: " + positive);
        System.out.println("Number of negative numbers: " + negative);
    }
}   

And here is the output for the given input:

Enter the array size: 
4
Enter the array values: 
23
-3
-2
21
Number of positive numbers: 2
Number of negative numbers: 2   

👉Write a class TwoArray to accept the number of rows and columns and array values, and display it all using control structures and array objects.👈

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates