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

Find the maximum & minimum number in an array of integers.

[HINT : Read about Integer.MIN_VALUE & Integer.MAX_VALUE in Java]


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

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


View

Input a string from the user. Create a new string called ‘result’ in which you will replace the letter ‘e’ in the original string with letter ‘i’. 

Example : 

original = “eabcdef’ ; result = “iabcdif”

Original = “xyz” ; result = “xyz”


View

Reverse a String (using StringBuilder class) in java.


View

Write a function to multiply 2 numbers.


View