🔀⚡Beginner
Check Positive, Negative, or Zero
Determine sign of a number
This program checks whether a number is positive (greater than 0), negative (less than 0), or zero. It demonstrates the use of if-else if-else conditional statements.
📚 What You Will Learn
- •Using
if-else if-elseladder - •Comparison operators (
>,<,==) - •Decision making based on conditions
C Program to Check Positive, Negative, or Zero
positive_negative_zero.c
C
1#include <stdio.h>23int main() {4 int num;5 6 // Get number from user7 printf("Enter a number: ");8 scanf("%d", &num);9 10 // Check using if-else if-else11 if (num > 0) {12 printf("%d is Positive\n", num);13 } 14 else if (num < 0) {15 printf("%d is Negative\n", num);16 } 17 else {18 printf("The number is Zero\n");19 }20 21 return 0;22}Output
Enter a number: -5
-5 is Negative
How if-else if-else Works
if (num > 0)First condition checked. If TRUE, execute and skip rest.
else if (num < 0)Checked only if first condition is FALSE.
elseExecutes if ALL above conditions are FALSE (num must be 0).
Decision Flow
num = -5
↓
num > 0 ? → FALSE
↓
num < 0 ? → TRUE
↓
Print "Negative"
Step-by-Step Execution:
1
Input
User enters -5, stored in num
2
First Check
num > 0 → -5 > 0 is FALSE, skip this block
3
Second Check
num < 0 → -5 < 0 is TRUE, execute this block
4
Output
Prints "-5 is Negative"
Comparison Operators in C
| Operator | Meaning | Example |
|---|---|---|
| > | Greater than | 5 > 3 → true |
| < | Less than | 3 < 5 → true |
| >= | Greater than or equal | 5 >= 5 → true |
| <= | Less than or equal | 3 <= 5 → true |
| == | Equal to | 5 == 5 → true |
| != | Not equal to | 5 != 3 → true |
⚠️ Common Mistakes
- • Using
=instead of==for comparison - • Missing curly braces for multi-line blocks
- • Order of conditions matters - most specific first
📝 Key Takeaways
✓Use
if-else if-else for multiple conditions✓
else is the default/fallback case✓Only ONE block executes in the ladder
✓Use
== not = for comparisonRelated Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials