Positive or Negative Number

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

BeginnerTopic: Module 1: Basic Java Programs
Back

Java Positive or Negative Number Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
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

Understanding Positive or Negative Number

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

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents