Java Program to Check Positive, Negative or Zero
Use a simple
if-else statement in Java to check if a number is positive, negative, or zero like this: if (num > 0) System.out.println("Positive"); else if (num < 0) System.out.println("Negative"); else System.out.println("Zero");.Examples
Input10
OutputPositive
Input-5
OutputNegative
Input0
OutputZero
How to Think About It
To decide if a number is positive, negative, or zero, first compare it with zero using
greater than and less than checks. If it's greater than zero, it's positive; if less, it's negative; otherwise, it must be zero.Algorithm
1
Get the input number.2
Check if the number is greater than zero.3
If yes, print 'Positive'.4
Otherwise, check if the number is less than zero.5
If yes, print 'Negative'.6
If neither, print 'Zero'.Code
java
public class CheckNumber { public static void main(String[] args) { int num = 10; // Change this value to test if (num > 0) { System.out.println("Positive"); } else if (num < 0) { System.out.println("Negative"); } else { System.out.println("Zero"); } } }
Output
Positive
Dry Run
Let's trace the input 10 through the code
1
Initialize number
num = 10
2
Check if num > 0
10 > 0 is true
3
Print result
Print 'Positive'
| Step | Condition | Result |
|---|---|---|
| Check if num > 0 | 10 > 0 | true |
| Check if num < 0 | Skipped | N/A |
| Print output | Positive | Printed |
Why This Works
Step 1: Compare number with zero
The program uses if (num > 0) to check if the number is positive.
Step 2: Check for negative
If the first check fails, else if (num < 0) checks if the number is negative.
Step 3: Handle zero
If neither condition is true, the number must be zero, so it prints Zero.
Alternative Approaches
Using ternary operator
java
public class CheckNumber { public static void main(String[] args) { int num = -3; String result = (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero"; System.out.println(result); } }
This approach is shorter but can be harder to read for beginners.
Using switch expression (Java 14+)
java
public class CheckNumber { public static void main(String[] args) { int num = 0; String result = switch (Integer.signum(num)) { case 1 -> "Positive"; case -1 -> "Negative"; default -> "Zero"; }; System.out.println(result); } }
Uses Java 14+ switch expressions and <code>Integer.signum()</code> for a modern style.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of comparisons regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a fixed amount of memory for variables and no extra data structures, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time and space; the difference is mainly readability and style.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-else statements | O(1) | O(1) | Clear and beginner-friendly |
| Ternary operator | O(1) | O(1) | Shorter code, less readable for beginners |
| Switch expression | O(1) | O(1) | Modern Java style, requires Java 14+ |
Always test with positive, negative, and zero values to cover all cases.
Beginners often forget to check the zero case separately, causing wrong output for zero input.