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

Write a function to multiply 2 numbers.


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

Take an array of names as input from the user and print them on the screen.


View

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


View

Write a function to calculate the product of 2 numbers.


View

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. 


View

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


View