Exercise
One or two negative numbers
Objetive
Write a java program to prompt the user for two numbers and determine whether both are negative, only one is negative, or neither is negative.
Example Code
import java.util.Scanner;
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner to read user input
Scanner scanner = new Scanner(System.in);
// Declare variables to store the two numbers entered by the user
int num1, num2;
// Prompt the user to enter the first number
System.out.print("Enter the first number: ");
num1 = scanner.nextInt();
// Prompt the user to enter the second number
System.out.print("Enter the second number: ");
num2 = scanner.nextInt();
// Check the conditions for both numbers being negative, one negative, or neither
if (num1 < 0 && num2 < 0) {
// If both numbers are negative, display this message
System.out.println("Both numbers are negative.");
} else if (num1 < 0 || num2 < 0) {
// If only one of the numbers is negative, display this message
System.out.println("One number is negative.");
} else {
// If neither of the numbers is negative, display this message
System.out.println("Neither of the numbers is negative.");
}
}
}