Understanding how to check whether a number is positive, negative, or zero is a fundamental concept in programming. In this post, we’ll explore how to implement this logic using C. This is one of the most common beginner exercises and lays the groundwork for decision-making structures in code.
Why Is This Important?
Knowing how to differentiate between positive, negative, and zero values is essential in many real-world applications. For instance, it’s useful in data validation, scientific computing, finance-related software (like tracking profit/loss), and control systems in embedded devices. Moreover, this forms the base for many conditional logic problems.
The C Program to Determine Number Sign
Below is a C program that takes an integer as input and tells you whether it’s positive, negative, or zero.
#include <stdio.h>
int main() {
int num;
// Input from user
printf("Enter a number: ");
scanf("%d", &num);
// Decision-making using if-else
if (num > 0) {
printf("The number is Positive.\n");
} else if (num < 0) {
printf("The number is Negative.\n");
} else {
printf("The number is Zero.\n");
}
return 0;
}
Sample Input and Output
Example 1
Input: 25
Output: The number is Positive.
Example 2
Input: -14
Output: The number is Negative.
Example 3
Input: 0
Output: The number is Zero.
How the Program Works
The logic is quite simple. First, the program takes an integer input from the user. Then, it checks the value using if
, else if
, and else
statements.
- If the number is greater than 0, it prints “Positive.”
- If it’s less than 0, it prints “Negative.”
- Otherwise, it prints “Zero.”
As you can see, the flow is based entirely on basic conditional statements in C.
Use Cases
This kind of check is often required in:
- Input filtering in UIs
- Validating user forms
- Financial calculations
- Gaming scoreboards
- Scientific computing and control applications