C Program to check if a Number Is Positive Or Negative

Check if a Number is Positive or Negative in C

Given an integer input, The objective is to write a code to Check if a Number is Positive or Negative in C Language.

Check if a Number is Positive or Negative in C

Check if a Number is Positive or Negative in C

Given an integer input, the objective is check whether the given integer is Positive or Negative. 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

We’ll discuss each method in-depth in the section below.

Check if a Number is Positive or Negative in C

Method 1: Using Brute Force

This method uses Brute Force to check whether a given integer is Positive or Negative.

C Code

Run
#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

Same logic we have followed in the below C program. 

Method 2: Using Nested if-else Statements

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

C Code

Run
#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

Same logic we have followed in the below C program. 

Method 3: Using Ternary Operators

This method uses a ternary operator in C to check whether a number is Positive or Negative.

C Code

Run
#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”);

Same logic we have followed in the below C program. 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 100+ courses offered by PrepInsta in One Subscription

Get over 150+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Getting Started

4 comments on “C Program to check if a Number Is Positive Or Negative”


  • Ashveen

    I purchased your prime Subscription and I have 4 offers. From Goldman Sachs, TCS Digital, Wipro and Capgemini
    You guys are amazing best course ever.


  • pavanicherla62

    #include
    int main()
    {
    int num;
    printf(” enter the number”);
    scanf(“%d”,&num);
    if (num >= 0)
    {
    printf(“positive”);
    }
    else
    {
    printf(” negative”);
    }
    return 0;
    }