C# Program to Check Positive, Negative, or Zero
Use
if statements in C# to check if a number is positive, negative, or zero like this: if (num > 0) { Console.WriteLine("Positive"); } else if (num < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); }.Examples
Input5
OutputPositive
Input-3
OutputNegative
Input0
OutputZero
How to Think About It
To decide if a number is positive, negative, or zero, first compare it with zero using
> and < operators. If it is greater than zero, it is positive; if less, it is negative; otherwise, it is zero.Algorithm
1
Get the input number.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
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int num = int.Parse(Console.ReadLine()); if (num > 0) { Console.WriteLine("Positive"); } else if (num < 0) { Console.WriteLine("Negative"); } else { Console.WriteLine("Zero"); } } }
Output
Enter a number: 5
Positive
Dry Run
Let's trace input 5 through the code
1
Input
User enters 5, so 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 |
| Print output | Positive | Printed |
Why This Works
Step 1: Compare with zero
The program uses if to check if the number is greater than zero to identify positive numbers.
Step 2: Check for negative
If the number is not positive, it checks if it is less than zero to find negative numbers.
Step 3: Handle zero
If neither positive nor negative, the number must be zero, so it prints 'Zero'.
Alternative Approaches
Using switch expression (C# 8.0+)
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int num = int.Parse(Console.ReadLine()); string result = num switch { > 0 => "Positive", < 0 => "Negative", _ => "Zero" }; Console.WriteLine(result); } }
This approach uses a modern switch expression for cleaner code but requires C# 8.0 or later.
Using ternary operator
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int num = int.Parse(Console.ReadLine()); string result = num > 0 ? "Positive" : (num < 0 ? "Negative" : "Zero"); Console.WriteLine(result); } }
This uses nested ternary operators for a compact form but can be harder to read for beginners.
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 does not allocate extra space, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time; the choice depends on readability and language version support.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-else statements | O(1) | O(1) | Beginners and clarity |
| Switch expression | O(1) | O(1) | Modern syntax, cleaner code |
| Ternary operator | O(1) | O(1) | Compact code, experienced users |
Always parse input carefully and handle invalid input to avoid errors.
Beginners often forget to handle the zero case separately, causing wrong output.