2. Write a program that will input 5 numbers and will determine if the numbers are positive or negative. The program should count and display the number of positive and negative numbers. Use while / do while / for loop.
Sun Dec 08 2024 11:34:26 GMT+0000 (Coordinated Universal Time)
Saved by
@John_Almer
package positivenegativecount;
import java.util.Scanner;
public class PositiveNegativeCount {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int positiveCount = 0, negativeCount = 0;
for (int i = 1; i <= 5; i++) {
System.out.print("Enter number " + i + ": ");
int num = input.nextInt();
if (num > 0) {
positiveCount++;
} else if (num < 0) {
negativeCount++;
}
}
System.out.println("Positive: " + positiveCount);
System.out.println("Negative: " + negativeCount);
}
}
content_copyCOPY
Comments