Exercise
Positive and negative
Objetive
Write a java program to get a number and answer whether it is positive or negative.
Example Code
// Importing the Scanner class to handle input from the useimport java.util.Scanner; // Import Scanner class for input
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Ask the user to input a number
System.out.print("Enter a number: ");
// Read the user's input as a double
double number = scanner.nextDouble();
// Check if the number is positive, negative, or zero
if (number > 0) {
// Display that the number is positive
System.out.println("The number is positive.");
} else if (number < 0) {
// Display that the number is negative
System.out.println("The number is negative.");
} else {
// Display that the number is zero
System.out.println("The number is zero.");
}
// Close the scanner to free up resources
scanner.close();
}
}