Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered. 


import java.util.*;

 

public class Solutions {

   public static void main(String args[]) {

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

       System.out.println("Press 1 to continue & 0 to stop");

       Scanner sc = new Scanner(System.in);

       int input = sc.nextInt();

 

       while(input == 1) {

           System.out.println("Enter your number : ");

           int number = sc.nextInt();

           if(number > 0) {

               positive++;

           } else if(number < 0) {

               negative++;

           } else {

               zeros++;

           }

 

           System.out.println("Press 1 to continue & 0 to stop");

           input = sc.nextInt();

       }

 

       System.out.println("Positives : "+ positive);

       System.out.println("Negatives : "+ negative);

       System.out.println("Zeros : "+ zeros);

   }   

}



Share to whatsapp

More Questions from Java Basic Codes Module 0

For a given matrix of N x M, print its transpose in java.


View

Two numbers are entered by the user, x and n. Write a function to find the value of one number raised to the power of another i.e. x^n.


View

Write a function to print the sum of all odd numbers from 1 to n.


View

Searching for an element x in a matrix.


View

Write a function that takes in age as input and returns if that person is eligible to vote or not. A person of age > 18 is eligible to vote.


View

Take an array of Strings input from the user & find the cumulative (combined) length of all those strings.


View

Write a function to calculate the product of 2 numbers.


View