C Program to Check Whether a Number is Positive or Negative

Determining whether a number is positive or negative is a fundamental task in C programming. This simple yet essential operation can be implemented in various ways, depending on the complexity and requirements of the program. In this article, we’ll explore three distinct methods to write a C program to check whether a number is positive or negative. We will provide detailed explanations, code examples, and outputs for each approach, helping you choose the most suitable method for your use case. You can also refer to our complete guide to learn C Program to Check Whether a Character is a Vowel or Consonant.

C Program to Check Whether a Number is Positive or Negative: A Complete Guide

Introduction

In mathematics, numbers are typically classified as positive, negative, or zero. In C programming, we can easily determine whether a given number falls into one of these categories using conditional checks. This operation is common in many programs, such as validating user inputs, performing calculations, or creating decision-making algorithms.

The task of checking whether a number is positive or negative can be achieved using various techniques, including basic control structures like if-else statements, the ternary operator, and modular code with functions. Let’s explore these methods in detail.

Method 1) C Program to Check Whether a Number is Positive or Negative Using If-Else Statements

Code Example

#include <stdio.h>

int main() {
    int number;

    // Ask the user to input a number
    printf("Enter a number: ");
    scanf("%d", &number);

    // Check if the number is positive, negative, or zero
    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;
}

Explanation

The most straightforward approach to check whether a number is positive or negative is by using the if-else statement. This method involves a simple comparison of the number with zero. If the number is greater than zero, it is positive; if it is less than zero, it is negative; otherwise, it is zero.

Output

Input: 10
Output: 10 is a positive number.
Input: -5
Output: -5 is a negative number.
Input: 0
Output: The number is zero.

Analysis

The if-else statement is a simple and intuitive way to handle basic conditional checks. It is easy to implement and works well for small programs, making it a great choice for beginners.

Method 2) C Program to Check Whether a Number is Positive or Negative Using the Ternary Operator

Code Example

#include <stdio.h>

int main() {
    int number;

    // Ask the user to input a number
    printf("Enter a number: ");
    scanf("%d", &number);

    // Use the ternary operator to check if the number is positive, negative, or zero
    (number > 0) ? printf("%d is a positive number.\n", number) : 
    (number < 0) ? printf("%d is a negative number.\n", number) : 
    printf("The number is zero.\n");

    return 0;
}

Explanation

The ternary operator offers a more concise alternative to the if-else statement. It is a single-line conditional operator that evaluates a condition and returns one of two values depending on the result. The syntax for the ternary operator is:

condiion ? expression1 : expression2;

In this case, we can use it to check whether the number is positive, negative, or zero.

Output

Input: 7
Output: 7 is a positive number.
Input: -2
Output: -2 is a negative number.
Input: 0
Output: The number is zero.

Analysis

The ternary operator is efficient for situations where you want to keep your code short and readable. It is particularly useful when you need to check a condition and print a result in a single line.

Method 3) C Program to Check Whether a Number is Positive or Negative Using Functions

Code Example

#include <stdio.h>

// Function to check if the number is positive, negative, or zero
void checkNumber(int 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");
    }
}

int main() {
    int number;

    // Ask the user to input a number
    printf("Enter a number: ");
    scanf("%d", &number);

    // Call the function to check the number
    checkNumber(number);

    return 0;
}

Explanation

Using functions to check if a number is positive or negative is a great way to modularize your code. By creating a separate function, you can easily reuse this check in different parts of your program without duplicating code.

Output

Input: 15
Output: 15 is a positive number.
Input: -3
Output: -3 is a negative number.
Input: 0
Output: The number is zero.

Analysis

By using a function to check the number, you achieve better code modularity and reusability. This is especially useful in larger programs where the number checking operation might be needed at multiple points.

Frequently Asked Questions (FAQs)

How to check if a number is negative or positive in C?

You can check if a number is positive, negative, or zero using an if-else statement or the ternary operator.

How to check if the number is positive or negative?

Use a conditional check to compare the number with zero:

  • Greater than zero → Positive
  • Less than zero → Negative
  • Equal to zero → Zero

How to define a negative number in C?

A negative number in C is any number less than zero. For example, int negative = -5;

How to make a number positive in C?

To convert a number to positive, you can use the absolute value function:

#include <stdlib.h>
int positive = abs(-5); // Output: 5

How to count positive and negative numbers in C?

You can loop through an array and count the positive and negative numbers using conditionals:

if (array[i] > 0) positiveCount++;
else if (array[i] < 0) negativeCount++;

Is a negative value false in C?

In C, any non-zero value (including negative values) is considered true in a boolean context. Only zero is considered false.

What do you call whether a number is positive or negative?

The property of a number being positive, negative, or zero is called its sign.

How do you know which one is positive or negative?

You can easily determine this by comparing the number to zero:

  • Positive: Greater than zero
  • Negative: Less than zero
  • Zero: Equal to zero

How to check floating-point numbers for positive or negative in C?

You can use the same methods for floating-point numbers, such as if-else statements or ternary operators.

What is the easiest way to check if a number is positive or negative in C?

The easiest way is using an if-else statement:

if (number > 0) printf("Positive");
else if (number < 0) printf("Negative");
else printf("Zero");

Conclusion on C Program to Check Whether a Number is Positive or Negative

In this article, we discussed three different methods for writing a C program to check whether a number is positive or negative. Whether you choose to use if-else statements, the ternary operator, or functions, each method provides a reliable solution for this common programming task. By understanding and applying these techniques, you can enhance your C programming skills and write cleaner, more efficient code.

Categories C

Leave a Comment