Exercise
Two negative numbers
Objetive
Write a java program to prompt the user for two numbers and determine if both numbers are negative or not.
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 if both numbers are negative
if (num1 < 0 && num2 < 0) {
// If both numbers are negative, display this message
System.out.println("Both numbers are negative.");
} else {
// If either or both numbers are not negative, display this message
System.out.println("Both numbers are not negative.");
}
}
}