Java program to check whether a number is positive or negative

Here's a Java program that checks whether a number is positive or negative:

import java.util.Scanner;

public class PositiveNegativeExample {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        if (num > 0) {
            System.out.println("The number is positive.");
        } else if (num < 0) {
            System.out.println("The number is negative.");
        } else {
            System.out.println("The number is zero.");
        }
    }
}
Sourc‮:e‬www.theitroad.com

Explanation:

The program first creates a Scanner object to read user input from the console. It then prompts the user to enter a number by printing the message "Enter a number: " to the console.

The nextInt method of the Scanner object is called to read the next integer value entered by the user and store it in the num variable.

The program then checks whether num is greater than zero, less than zero, or equal to zero using an if-else-if statement. If num is greater than zero, the program prints "The number is positive." to the console. If num is less than zero, the program prints "The number is negative." to the console. If num is equal to zero, the program prints "The number is zero." to the console.

Note that this program assumes that the user will enter a valid integer value. If the user enters a non-integer value, the program will throw a java.util.InputMismatchException. To handle this case, you could wrap the nextInt call in a try-catch block to catch the exception and handle it appropriately.