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

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


View

Write a program to print Fibonacci series of n terms where n is input by user :

0 1 1 2 3 5 8 13 21 ..... 

In the Fibonacci series, a number is the sum of the previous 2 numbers that came before it.


View

Write a function which takes in 2 numbers and returns the greater of those two.


View

Write a function that calculates the Greatest Common Divisor of 2 numbers.


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

Enter 3 numbers from the user & make a function to print their average.


View

Searching for an element x in a matrix.


View