C Program to Check Whether a Given Integer is Positive or Negative

A number is positive if it is greater than zero, negative if it is less than zero, and zero is neither. This is one of the most fundamental checks in programming — it shows up in input validation, physics simulations, financial calculations, and signal processing. This program handles all three cases with a clear if-else chain.

Input Condition Result
5 n > 0 Positive
−3 n < 0 Negative
0 else Zero (neither)

C Program: Positive, Negative, or Zero

/* Check whether a given integer is positive, negative, or zero
 * Compile: gcc -ansi -Wall -Wextra posneg.c -o posneg */
#include <stdio.h>

int main(void)
{
    int n;
    printf("Enter an integer: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input.\n");
        return 1;
    }

    if (n > 0)
        printf("%d is Positive.\n", n);
    else if (n < 0)
        printf("%d is Negative.\n", n);
    else
        printf("You entered 0 (neither positive nor negative).\n");

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra posneg.c -o posneg
./posneg

Sample Output

Enter an integer: 5
5 is Positive.

Enter an integer: -3
-3 is Negative.

Enter an integer: 0
You entered 0 (neither positive nor negative).

Code Explanation

  • Three-way if-else chainif (n > 0) handles positive, else if (n < 0) handles negative, and the final else catches zero. This structure exhausts all integer cases: any integer satisfies exactly one of n>0, n<0, or n==0. Using else if is more efficient than independent if checks because once a branch matches, the rest are skipped.
  • Zero is neither positive nor negative — a common beginner mistake is treating 0 as positive (because it is not negative) or ignoring it entirely. In mathematics, zero belongs to neither set. Explicitly handling zero with a descriptive message prevents misleading output.
  • scanf return checkscanf("%d", &n) returns 1 on success, 0 on mismatch, EOF on end of input. Checking != 1 guards against non-integer input. Without the check, n is uninitialized and the comparison result is undefined behavior.

What This Program Teaches

  • if-else if-else chains — the canonical pattern for mutually exclusive cases. Once one branch evaluates to true, no subsequent branch is checked. Order matters: put the most frequent or most specific cases first.
  • Correct treatment of zero — zero is a boundary value that breaks many naive programs. Always enumerate your boundary conditions explicitly rather than letting them fall into a catch-all “else” with a misleading label.
  • Defensive input reading — checking the return of scanf is a real-world habit. Production C code that reads user input always validates before using the read value — the same discipline applies whether you are reading from a terminal, a file, or a network socket.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

2 comments on “C Program to Check Whether a Given Integer is Positive or Negative

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>