Q:

Write a Java program that inputs five real numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input

0

Write a Java program that inputs five real numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int negativeCount = 0;
        int positiveCount = 0;
        int zeroCount = 0;

        System.out.println("Enter five real numbers:");

        for (int i = 0; i < 5; i++) {
            if (scanner.hasNextDouble()) {
                double number = scanner.nextDouble();
                if (number < 0) {
                    negativeCount++;
                } else if (number > 0) {
                    positiveCount++;
                } else {
                    zeroCount++;
                }
            } else {
                System.out.println("Invalid input. Expected five real numbers.");
                break;
            }
        }

        System.out.println("Number of negative numbers: " + negativeCount);
        System.out.println("Number of positive numbers: " + positiveCount);
        System.out.println("Number of zeros: " + zeroCount);
    }
}

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now