The program prompts the user to enter a number and keeps a count of the positive, negative, and zero numbers entered until the user chooses to stop. At the end, the program prints out the count of each category of numbers.
import java.util.Scanner; class Count_PosNegZero { public static void main(String[] args) { Scanner input = new Scanner(System.in); int c_pos = 0,c_neg = 0,c_zero = 0; char choice; do { System.out.print("Enter the number "); int number = input.nextInt(); if(number > 0) { c_pos++; } else if(number < 0) { c_neg++; } else { c_zero++; } System.out.print("Do you want to Continue y/n? "); choice = input.next().charAt(0); }while(choice=='y' || choice == 'Y'); System.out.println("Positive numbers: " + c_pos); System.out.println("Negative numbers: " + c_neg); System.out.println("Zero numbers: " + c_zero); } }
Enter the number 23 Do you want to Continue y/n? y Enter the number -34 Do you want to Continue y/n? y Enter the number 0 Do you want to Continue y/n? y Enter the number 43 Do you want to Continue y/n? y Enter the number -3 Do you want to Continue y/n? y Enter the number -45 Do you want to Continue y/n? n Positive numbers: 2 Negative numbers: 3 Zero numbers: 1
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions