Positive or Negative Number

Check whether a number is positive, negative, or zero.

JavaBeginner
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        double n = sc.nextDouble();

        if (n > 0) {
            System.out.println(n + " is Positive");
        } else if (n < 0) {
            System.out.println(n + " is Negative");
        } else {
            System.out.println("Number is Zero");
        }

        sc.close();
    }
}

Output

Enter a number: -3
-3.0 is Negative

We compare the number with 0 to classify it as positive, negative, or zero.