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 chain —
if (n > 0)handles positive,else if (n < 0)handles negative, and the finalelsecatches zero. This structure exhausts all integer cases: any integer satisfies exactly one of n>0, n<0, or n==0. Usingelse ifis more efficient than independentifchecks 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 check —
scanf("%d", &n)returns 1 on success, 0 on mismatch, EOF on end of input. Checking!= 1guards against non-integer input. Without the check,nis 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
scanfis 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
- Odd or Even Check in C
- Coordinate Quadrant in C
- Leap Year Check in C
- Quadratic Equation Roots in C
- Sum of N Natural Numbers in C
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”
Hey, if I enter 0 as input,
Your code gives the output as Negative integer.
Please implement the if condition as if(number < 0) then print negative. also if 0 is entered it should print Neither +ve nor -ve.
Hello, Thanks for pointing out. I have fixed that.
-Sandeepa