Saturday, 8 August 2026

Checking Positive and Negative Numbers

 Checking if a number is positive or negative in C is a fundamental skill for any budding programmer. This simple program to find whether a number is positive or negative in C introduces essential concepts like conditional statements and user input handling. 

Understanding how to implement this logic is crucial for real-world applications such as validating user data, controlling program flow, and developing algorithms used in finance, engineering, and data analysis. 

By mastering this program, you'll build a strong foundation for more complex C programming challenges.

Coders' comments

Numbers defined by C, whether high or low, Master the code and watch your career grow.

Option 1: C Program to Check Whether a Number is Positive or Negative

#include <stdio.h>

int main() {
    
    // Declare a variable of integer type
    int number;

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

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

    return 0;
}

Output:

Enter a number: 7
7 is a positive number.

Explanation:

  • Include Standard I/O Library: #include <stdio.h> allows the program to use printf() for output and scanf() for input handling.

  • Variable Declaration: The int number; variable is declared to store the user-input number.

  • User Input: scanf("%d", &number); reads an integer from the user and stores it in the number variable.

  • Conditional Logic:

  • Positive Check: If number > 0, it is positive, and printf() displays that it is a positive number.

  • Negative Check: If number < 0, it is negative, and printf() outputs that it is a negative number.

  • Zero Check: If neither condition is met, the number is zero, and printf() confirms it.

  • Display Result: Based on the conditions checked, the program outputs whether the number is positive, negative, or zero.

Time Complexity Analysis

Time Complexity : O(1)

Space Complexity Analysis

Space Complexity : O(1)

Option 2: Number is Positive or Negative in C Using Ternary Operator

#include <stdio.h>

int main() {
    
    // Declare an integer type variable
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

    // Check number is positive or negative 
    // using ternary operator
    (number > 0) ? printf("%d is positive.\n", number) :
    (number < 0) ? printf("%d is negative.\n", number) :
    printf("The number is zero.\n");

    return 0;
}

Output:

Enter a number: -5
-5 is negative.

Explanation:

This C program to check number is positive, negative, or zero uses the ternary operator for a concise, three-way check. The ternary operator evaluates each condition and outputs whether the given number is positive, negative, or zero in C, making the code compact and efficient.

Time Complexity Analysis

Time Complexity : O(1)

Space Complexity Analysis

Space Complexity : O(1)

Option 3: Check Positive or Negative Number in C Using Nested if-else

#include <stdio.h>

int main() {
    
    // Declare an integer type variable
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

    // Check number is positive or negative 
    //using nested if-else
    if (number >= 0) {
        if (number == 0)
            printf("The number is zero.\n");
        else
            printf("%d is positive.\n", number);
    } else {
        printf("%d is negative.\n", number);
    }

    return 0;
}

Output:

Enter a number: 0
The number is zero.

Explanation:

This C program to check number is positive or negative uses nested if-else statements to evaluate whether the given number is positive, negative, or zero in C. The outer if checks if the number is non-negative, and the inner if specifies zero.

Time Complexity Analysis

Time Complexity : O(1)

Space Complexity Analysis

Space Complexity : O(1)

Option 4: Positive-Negative Number in C Using Switch Statement

#include <stdio.h>

int main() {
    
    // Declare an integer type variable
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

    // Check if the number is positive or negative 
    // using switch cases
    switch ((number > 0) - (number < 0)) {
        case 1:
            printf("%d is positive.\n", number);
            break;
        case -1:
            printf("%d is negative.\n", number);
            break;
        case 0:
            printf("The number is zero.\n");
            break;
    }

    return 0;
}

Output:

Enter a number: 10
10 is positive.

Explanation:

In this C program to check if a number is positive or negative, the switch statement evaluates (number > 0) - (number < 0), which gives 1 for positive, -1 for negative, and 0 for zero. This method uses a unique approach to determine whether the given number is positive, negative, or zero in C.

Time Complexity Analysis

Time Complexity : O(1)

Space Complexity Analysis

Space Complexity : O(1)

Option 5: C Program to Check Positive or Negative Using Function

#include <stdio.h>

// Function to check number
void checkNumber(int num) {
    if (num > 0)
        printf("%d is positive.\n", num);
    else if (num < 0)
        printf("%d is negative.\n", num);
    else
        printf("The number is zero.\n");
}

int main() {
    int number;

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

    // Calling the function
    checkNumber(number);

    return 0;
}

Output:

Enter a number: 10
10 is positive.

Explanation:

  • This program uses a separate function to check whether the number is positive, negative, or zero, instead of writing all logic inside main().

  • The input is taken in the main function and passed to checkNumber(), which performs the condition checks and prints the result.

  • This approach makes the code more organized and reusable, especially when the same logic is needed multiple times in a program.

Time Complexity Analysis

Time Complexity : O(1)

Space Complexity Analysis

Space Complexity : O(1)


No comments:

Post a Comment