C Program to Check Whether a Number is Positive or Negative or Zero.

This program takes a number from user and checks whether that number is either positive or negative or zero.

Source Code


#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<=0)
    {
        if (num==0)
          printf("You entered zero.");
        else
          printf("%.2f is negative.",num);
    }
    else
      printf("%.2f is positive.",num);
    return 0;
}

This program also can be solved using nested if else statement.


/* C programming code to check whether a number is negative or positive or zero using nested if...else statement. */

#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<0)      /* Checking whether num is less than 0*/
      printf("%.2f is negative.",num);
    else if (num>0)   /* Checking whether num is greater than zero*/
      printf("%.2f is positive.",num);
    else
      printf("You entered zero.");
    return 0;
}

Output 1


Enter a number: 12.3
12.30 is positive.

Output 2


Enter a number: -12.3
-12.30 is negative.

Output 3


Enter a number: 0
You entered zero.