0
0
CProgramBeginner · 2 min read

C Program to Check Positive, Negative or Zero

In C, you can check if a number is positive, negative, or zero using if statements like if (num > 0) for positive, else if (num < 0) for negative, and else for zero.
📋

Examples

Input5
OutputPositive number
Input-3
OutputNegative number
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 input number from the user
2
Check if the number is greater than zero
3
If yes, print 'Positive number'
4
Else check if the number is less than zero
5
If yes, print 'Negative number'
6
Otherwise, print 'Zero'
💻

Code

c
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    if (num > 0) {
        printf("Positive number\n");
    } else if (num < 0) {
        printf("Negative number\n");
    } else {
        printf("Zero\n");
    }
    return 0;
}
Output
Enter a number: 5 Positive number
🔍

Dry Run

Let's trace the 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 number'

StepConditionResult
Check if num > 05 > 0true
Check if num < 05 < 0false
Else-Print 'Positive number'
💡

Why This Works

Step 1: Input reading

The program reads an integer from the user using scanf.

Step 2: Positive check

It uses if (num > 0) to check if the number is positive.

Step 3: Negative and zero check

If not positive, it checks else if (num < 0) for negative, else it must be zero.

🔄

Alternative Approaches

Using switch with sign function
c
#include <stdio.h>

int sign(int n) {
    return (n > 0) - (n < 0);
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    switch(sign(num)) {
        case 1:
            printf("Positive number\n");
            break;
        case -1:
            printf("Negative number\n");
            break;
        default:
            printf("Zero\n");
    }
    return 0;
}
This uses a helper function and switch-case for clarity but is slightly more complex.
Using ternary operator
c
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    (num > 0) ? printf("Positive number\n") : (num < 0) ? printf("Negative number\n") : printf("Zero\n");
    return 0;
}
This is a compact form using ternary operators 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 no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; the simple if-else is easiest to read and fastest to write.

ApproachTimeSpaceBest For
If-else statementsO(1)O(1)Simplicity and clarity
Switch with sign functionO(1)O(1)Structured code with switch-case
Ternary operatorO(1)O(1)Compact code, less readable
💡
Always test your program with positive, negative, and zero inputs to cover all cases.
⚠️
Beginners often forget to use else if and write separate if statements causing multiple outputs.