C Program to check whether the given integer is positive or negative

By | August 22, 2020

C Program to check whether the given integer is positive or negative.

Examples :

Input: 50
Output: Number is Positive.

Input: -25
Output: Number is Negative.

Input: 0
Output: Number is Positive nor Negative, i.e. 0 

Approach used :
A number is said negative if it is less than 0 i.e. num < 0 and said positive if it is greater than 0 i.e. num > 0. Otherwise niether positive nor negative i.e., 0.

  1. Input a number from user in some variable say num.
  2. Check if(num < 0), then number is negative.
  3. Check if(num > 0), then number is positive.
  4. Check if(num == 0), then number is zero.

Let us code solution of the program.

Implementation :

1. Using if condition –

// using if condition
#include <stdio.h>

int main()
{
    // take input
    int num = 50;    
    
    // for positive number
    if(num > 0)
    {
        printf("Number is Positive.");
    }
    // for negaitive number
    if(num < 0)
    {
        printf("Number is Negative.");
    }
    // for 0
    if(num == 0)
    {
        printf("Number is Positive nor Negative.");
    }

    return 0;
}

Output :

Number is Positive. 

2. Using if … else condition –

// using if ... else condtion
#include <stdio.h>

int main()
{
    // take input
    int num = 50;    
    

     if (num <= 0) {
         
         // for 0
        if (num == 0)
            printf("Number is Positive nor Negative, i.e. 0");
        else
            //for negative number
            printf("Number is Negative.");
    } else
    
        //for positive number
        printf("Number is Positive.");

    return 0;
}

Output :

Number is Positive. 



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.