Java – Check if a Number is Positive or Negative

A number is positive if it is greater than 0, and negative if it is less than 0. Zero is neither positive nor negative, so a complete Java program should handle all three possible cases.

In Java, the comparison operators > and < can be used with an if statement to determine whether a number is positive or negative. An else block can then handle zero.

</>
Copy
if (number > 0) {
    // number is positive
} else if (number < 0) {
    // number is negative
} else {
    // number is zero
}

How Java Determines Whether a Number Is Positive, Negative, or Zero

The decision is based on comparing the number with zero:

  • If number > 0 is true, the number is positive.
  • If number < 0 is true, the number is negative.
  • If both comparisons are false, the number must be zero.
NumberComparison with zeroResult
88 > 0Positive
-9-9 < 0Negative
0Neither > 0 nor < 0Zero

Check Positive or Negative Number using simple If Statement

To check if a number is positive, use comparison operator: greater than (>) that accepts the number and zero as operands. If number is greater than zero, it returns true, else it returns false.

To check if a number is negative, use comparison operator: less than (<) that accepts the number and zero as operands. If number is less than zero, it returns true, else it returns false.

PositiveNegative.java

</>
Copy
/**
 * Java Program - Check if Number is Positive or Negative
 */

public class PositiveNegative {

	public static void main(String[] args) {
		//number
		int num = 8;
		
		//check if number is positive
		if(num > 0) {
			System.out.println(num+" is positive.");
		}
		
		//check if number is negative
		if(num < 0) {
			System.out.println(num+" is negative.");
		}
	}
}

Output

8 is positive.

Here, num is 8. The condition num > 0 evaluates to true, so the first if block prints that the number is positive. The condition num < 0 is false, so its block is skipped.

With these two independent if statements, no message is printed when num is 0. Use an if-else-if-else statement when zero also needs to be identified explicitly.

Check Positive, Negative, or Zero using If-Else-If Statement

We shall use the same logic from above example, to check if given number is positive and negative. But with one change, which is: using if-else-if statement instead of simple if statement. We are using if-else-if statement because, if a number is positive, there is no chance that it could be negative and vice versa.

Also, we shall include an else block. If the number is neither positive nor negative, then else block will execute.

PositiveNegative.java

</>
Copy
/**
 * Java Program - Check if Number is Positive or Negative
 */

public class PositiveNegative {

	public static void main(String[] args) {
		//number
		int num = -9;
		
		//check if number is positive or negative
		if(num > 0) {
			System.out.println(num+" is positive.");
		} else if(num < 0) {
			System.out.println(num+" is negative.");
		} else {
			System.out.println(num+" is zero.");
		}
	}
}

Output

-9 is negative.

For num = -9, the first condition num > 0 is false. Java then evaluates num < 0, which is true, and prints that -9 is negative. Once a condition in the chain succeeds, the remaining branches are skipped.

Java Program to Check Positive, Negative, or Zero from User Input

Instead of assigning a fixed value in the program, you can read a number from the user with Scanner and apply the same comparisons.

</>
Copy
import java.util.Scanner;

public class PositiveNegative {
    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(num + " is positive.");
        } else if (num < 0) {
            System.out.println(num + " is negative.");
        } else {
            System.out.println(num + " is zero.");
        }

        scanner.close();
    }
}

For example, entering -25 produces the following result.

Enter a number: -25
-25 is negative.

What Happens When the Java Number Is Zero?

Zero requires its own case because neither 0 > 0 nor 0 < 0 is true. In an if-else-if-else chain, Java reaches the final else block when the value is zero.

</>
Copy
int num = 0;

if (num > 0) {
    System.out.println("Positive");
} else if (num < 0) {
    System.out.println("Negative");
} else {
    System.out.println("Zero");
}

Output

Zero

Check Only Whether a Number Is Negative in Java

If your program only needs to know whether a value is negative, test it directly with number < 0. There is no need for a three-way conditional when the positive and zero cases do not need separate handling.

</>
Copy
int number = -4;

if (number < 0) {
    System.out.println("The number is negative.");
}

Output

The number is negative.

Checking Positive and Negative Decimal Numbers in Java

The same comparison logic also works with decimal numeric types such as double. Compare the value with 0 just as you would for an integer.

</>
Copy
double number = -3.75;

if (number > 0) {
    System.out.println(number + " is positive.");
} else if (number < 0) {
    System.out.println(number + " is negative.");
} else {
    System.out.println(number + " is zero.");
}

Output

-3.75 is negative.

Common Mistakes When Checking Positive or Negative Numbers in Java

  • Ignoring zero: A condition such as num > 0 distinguishes positive values only. It does not make every other value negative because zero is a separate case.
  • Using >= 0 for positive numbers: This includes zero. Use > 0 when the requirement is strictly positive.
  • Using <= 0 for negative numbers: This also includes zero. A strictly negative number must satisfy < 0.
  • Writing unnecessary independent checks: When exactly one of positive, negative, or zero should be reported, an if-else-if-else chain expresses the mutually exclusive cases clearly.

Positive or Negative Number Check in Java – Key Result

In this Java Tutorial, we learned how to check if a number is positive or negative using Java comparison operators: less than and greater than.

The complete condition is straightforward: use number > 0 for a positive value, number < 0 for a negative value, and the remaining case for zero. An if-else-if-else statement is generally the clearest choice when all three results need to be reported.