C++ Program to Check Positive, Negative or Zero
Use a simple
if-else structure in C++ to check if a number is positive, negative, or zero, for example: if (num > 0) cout << "Positive"; else if (num < 0) cout << "Negative"; else cout << "Zero";.Examples
Input5
OutputPositive
Input-3
OutputNegative
Input0
OutputZero
How to Think About It
To decide if a number is positive, negative, or zero, compare it with zero using
greater than and less than checks. If it is greater than zero, it is positive; if less, it is negative; otherwise, it is zero.Algorithm
1
Get the input number from the user.2
Check if the number is greater than zero.3
If yes, print 'Positive'.4
Else, check if the number is less than zero.5
If yes, print 'Negative'.6
Otherwise, print 'Zero'.Code
cpp
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; if (num > 0) { cout << "Positive" << endl; } else if (num < 0) { cout << "Negative" << endl; } else { cout << "Zero" << endl; } return 0; }
Output
Enter a number: 5
Positive
Dry Run
Let's trace input 5 through the code
1
Input
User enters num = 5
2
Check if num > 0
5 > 0 is true
3
Print result
Print 'Positive'
| Step | Condition | Result |
|---|---|---|
| Check num > 0 | 5 > 0 | true |
| Check num < 0 | 5 < 0 | false |
| Print output | - | Positive |
Why This Works
Step 1: Input number
We first get the number from the user using cin.
Step 2: Check positivity
If the number is greater than zero using if (num > 0), it is positive.
Step 3: Check negativity
If not positive, check if it is less than zero with else if (num < 0) to identify negative numbers.
Step 4: Zero case
If neither positive nor negative, the number must be zero, so print 'Zero'.
Alternative Approaches
Using switch with sign function
cpp
#include <iostream> using namespace std; int sign(int n) { return (n > 0) - (n < 0); } int main() { int num; cout << "Enter a number: "; cin >> num; switch(sign(num)) { case 1: cout << "Positive" << endl; break; case -1: cout << "Negative" << endl; break; default: cout << "Zero" << endl; } return 0; }
This uses a helper function and switch-case for clarity but is slightly more complex.
Using ternary operator
cpp
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number: "; cin >> num; cout << (num > 0 ? "Positive" : (num < 0 ? "Negative" : "Zero")) << endl; return 0; }
This is a compact one-line decision using nested ternary operators, good for short code.
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
Only a few variables are used, so the space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in O(1) time; the if-else method is simplest and most readable for beginners.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-else | O(1) | O(1) | Simplicity and clarity |
| Switch with sign function | O(1) | O(1) | Structured code with switch-case |
| Ternary operator | O(1) | O(1) | Compact code, less readable for beginners |
Always test your program with positive, negative, and zero inputs to cover all cases.
Beginners often forget to handle the zero case separately, causing incorrect output.