Computer Applications

Define a class to accept the elements of an array from the user and check for the occurrence of positive number, negative number and zero.

Example

Input Array: 12, -89, -56, 0, 45, 56

Output:
3 Positive Numbers
2 Negative Numbers
1 Zero

Java

Java Arrays

4 Likes

Answer

import java.util.Scanner;

public class KboatSDANumbers
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int pc = 0;
        int nc = 0;
        int zc = 0;
        
        System.out.print("Enter size of array : ");
        int l = in.nextInt();
        int arr[ ] = new int[l];
        
        System.out.println("Enter array elements : ");
        for (int i = 0; i < l; i++) {
            arr[i] = in.nextInt();
        }
        
        for (int i = 0; i < l; i++) {
            if (arr[i] < 0)
                nc++;
            else if(arr[i] > 0)
                pc++;
            else
                zc++;
        }
        
        System.out.println(pc + " Positive Numbers");
        System.out.println(nc + " Negative Numbers");
        System.out.println(zc + " Zero");
    }
}

Output

Answered By

3 Likes


Related Questions