Check if a Number is Positive or Negative in C

Check if a Number is Positive or Negative

Given an integer input, the objective is Check if a Number is Positive or Negative in C. In order to do so we have the following methods:

  1. Method 1: Using Brute Force
  2. Method 2: Using Nested if-else Statements
  3. Method 3: Using the ternary operator
Check if a Number is Positive or Negative in C

Method 1: Using Brute Force

C Code

#include <stdio.h>
int main()
{
    int num = 23;
      
    //Conditions to check if the number is negative/positive or zero
    if (num > 0)
         printf("The number is positive");
    else if (num < 0)
        printf("The number is negative");
    else
        printf("Zero");
    
    return 0;
}

Output

Insert a number: 23
The number is Positive

Algorithm

For a user input num

  • If the num > 0: it is a positive number.
  • If the num < 0: it is a positive number.
  • Else the number has to be zero itself

Method 2: Using Nested if-else Statements

C Code

#include <stdio.h>
int main()
{
    int num = -10;
    
    //Condition to check if num is negative/positive or zero
    if (num >= 0)
    {
        if (num == 0)
            printf("The number is 0");
        else
            printf("The number is Positive");
    }
    else
        printf("The number is Negative");
    
    return 0;
}

Output

Insert a number: -10
The number is Negative

Algorithm

This method uses a nested if-else Statements to check whether a given number is Positive or Negative. For a user input num

  • If the num >= 0
    • If num == 0 : num is zero
    • Else number has to be positive 
  • Else the number has to be negative

Method 3: Using Ternary Operators

C Code

#include <stdio.h>
int main()
{
    int num = -4;
    
    //Condition to check if the 0, positive or negative
    
    if(num == 0)
        printf("Zero");
    else
        (num > 0) ? printf("Positive"): printf("Negative");
    
    return 0;
}

Output

Insert a number: -4
Negative

Algorithm

This method uses a ternary operator to check whether a number is Positive or Negative. For a user input num

  • If the num == 0 num is zero
  • Else (num > 0) ? printf(“Positive”): printf(“Negative”);

Also read:
GTA V Cheat Codes for PS4

Previous Article

ASCII Table

Next Article
Check if a Number is Positive or Negative

Check if a Number is Positive or Negative in C++

Related Posts