Thursday, 21 August 2014

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



Here is a simple C Program to Check Whether a Number is Positive or Negative or Zero is given below.This program simply use if conditional statement and find out the number is Positive or Negative or Zero.As we know that the number which is greater than zero is called positive number and which is less than zero is called negative number so this logic is used in this program.This program takes a number from user and checks whether that number is either positive or negative or zero.

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

Program 1


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

Program 2



#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: 45.23
45.23 is positive.
Output 2

Enter a number: -18.3
-18.30 is negative.
Output 3

Enter a number: 0
You entered zero.