Computer Science, asked by singhvinodavadhcolla, 1 month ago

write the program to enter 'n' number of integer and display the count of positive count of negative and the count of zeros entered by user​

Answers

Answered by RedProgrammer
0

Answer:

import java.util.Scanner;  

public class Main {  

   public static void main(String[] args) {

 

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter n numbers : ");

       int n = sc.nextInt();

       int negative = 0, positive = 0, zeros = 0;

       while(n-- > 0) {

           System.out.print("Enter a number: ");

           int number = sc.nextInt();

           if(number == 0) {

               zeros++;

           }

           else if(number < 0) {

               negative++;

           }else if(number > 0) {

               positive++;

           }

       }

       System.out.println("Negative number count : " + negative);

       System.out.println("Positive number count : " + positive);

       System.out.println("Zero number count : " + zeros);

   }

}

Explanation:

  1. We get the 'n' number from the user
  2. Then we initialize three variables, 'positive' (to store positive number count), 'negative' (to store negative number count) and 'zeros' (to store the zeros)
  3. Then we check 'n' numbers one by one, which is inputted by the user, if the number equals zero then we increment the zero count, else if the number is less than zero we increment the negative count, or else if the number is more than zero we increment the positive count.
  4. Lastly, we display the number count.
Similar questions