🔀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-else ladder
  • Comparison operators (>, <, ==)
  • Decision making based on conditions

C Program to Check Positive, Negative, or Zero

positive_negative_zero.c
C
1#include <stdio.h>
2
3int main() {
4 int num;
5
6 // Get number from user
7 printf("Enter a number: ");
8 scanf("%d", &num);
9
10 // Check using if-else if-else
11 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.

else

Executes 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

OperatorMeaningExample
>Greater than5 > 3 → true
<Less than3 < 5 → true
>=Greater than or equal5 >= 5 → true
<=Less than or equal3 <= 5 → true
==Equal to5 == 5 → true
!=Not equal to5 != 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 comparison