C Program to Check if a Number is Positive - Negative or Zero

C Program to Check if a Number is Positive – Negative or Zero

In C programming, checking whether a number is positive, negative, or zero is one of the first exercises for beginners. This program teaches how to use conditional statements effectively and introduces the basic concepts of input, output, and logical thinking. Understanding how to classify numbers is essential because it lays the foundation for decision-making in more complex programs, such as calculations, loops, and error handling.

This tutorial will guide you step by step on writing a C program that checks a number’s sign. We will explain every line of code in detail, discuss common mistakes beginners make, and show best practices for clarity and accuracy. By the end, you will be able to write programs that can evaluate any integer and determine whether it is positive, negative, or zero.

Using if-else Statements to Check Number Sign

The simplest way to determine if a number is positive, negative, or zero in C is by using the if-else conditional statements. Here is a complete program:

#include <stdio.h>

int main() {

    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    if (number > 0) {
        printf("%d is a positive number.\n", number);
    } else if (number < 0) {
        printf("%d is a negative number.\n", number);
    } else {
        printf("The number is zero.\n");
    }

    return 0;

}

In this program, the first line #include <stdio.h> includes the standard input-output library, allowing us to use printf() and scanf(). We declare an integer variable number to store the user’s input. The scanf("%d", &number); function reads the value entered by the user.

The if statement checks whether the number is greater than zero. If true, it prints that the number is positive. If not, the else if statement checks whether the number is less than zero, printing that it is negative. Finally, the else block executes if the number is neither positive nor negative, indicating that it is zero. This structure ensures that all possible cases for an integer are handled correctly.

Using Nested Conditional Operators (Optional)

Another method to classify numbers is by using the ternary conditional operator ?:. While less common for beginners, it can be useful for writing compact code:

#include <stdio.h>

int main() {

    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    (number > 0) ? printf("%d is positive.\n", number) :
    (number < 0) ? printf("%d is negative.\n", number) :
                   printf("The number is zero.\n");

    return 0;

}

Here, the ternary operator evaluates a condition and returns a result based on whether it is true or false. The first condition (number > 0) checks if the number is positive. If false, the second condition (number < 0) checks if it is negative. If both are false, the number is zero. This approach reduces multiple lines of code but can be slightly harder to read for beginners.

Common Beginner Mistakes

Beginners often make these mistakes when working with numbers:

  • Forgetting that zero is a special case. Zero is neither positive nor negative, so it should be handled separately if needed.
  • Using the wrong comparison operator, such as = instead of ==, which leads to logic errors.
  • Not validating user input. If a user enters a non-integer value, the program may behave unexpectedly or crash.

To avoid these issues, always handle zero explicitly, use == for comparisons, and carefully validate input from the user.

FAQs

Q1: Can this program handle floating-point numbers?
Yes, you can modify the program by changing the data type to float or double. The comparison logic remains the same.

Q2: Why do we use else if instead of multiple if statements?
Using else if ensures that only one block executes. Multiple if statements could execute multiple blocks unnecessarily, causing incorrect output.

Q3: Can I use a switch statement instead?
No, switch in C works only with discrete values like integers or characters, but not with relational conditions like > 0 or < 0.

Q4: Is it better to use ternary operators or if-else?
For beginners, if-else is more readable and easier to understand. Ternary operators are compact but less intuitive.

Conclusion

Checking if a number is positive, negative, or zero is a fundamental exercise in C programming. It introduces you to conditional statements and logical reasoning. By understanding and practicing this program, you will be prepared for more advanced decision-making tasks, loops, and mathematical operations in C. Start with simple programs like this, and gradually move to more complex programs to build confidence and skill in C programming.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. TutorialsPoint: C Operators – Guide to relational and logical operators.
  3. Programiz: Beginner C Programs – Collection of beginner-friendly examples.

Scroll to Top