C program to find Positive or Negative Number

How to write C program to find Positive or Negative Number or zero using If Else Statement, Else If, and Conditional Operator with an example?.

If the number is greater than 0, then the given number is positive, and if it is less than 0, then it is a negative number. If the number is equal to 0, then the given number is 0.

C program to find Positive or Negative Number using Nested If Condition

This program lets the user enter any number, and then this c program checks whether the entered value is either positive or negative or zero using Nested If

#include <stdio.h>

int main()
{
  int number;
  
  printf("Enter any number:\n");
  scanf("%d",&number);
 
  if (number >= 0)
  {
    if (number > 0)
      printf("%d is Positive Number", number);
    else
      printf("You have entered Value zero.");
  }
  else
    printf("%d is Negative Number", number);
 
 return 0;
}

C Positive or Negative output

Enter any number:
223
223 is Positive Number

Check for Negative Value

Enter any number:
-3
-3 is Negative Number

Check for Zero

Enter any number:
0
You have entered Value zero.

In this program to find Positive or Negative Number, First, if condition checks whether the given number is greater than or equal to 0.

  • If the condition fails, then the given number will be negative.
  • And, If the condition is true, then we have to check whether the number is greater than 0 or not. If the number is greater than 0, then the given number is positive. Else the value we entered is 0.

C program to find Positive or Negative Number using Else If

This program lets the user enter any number and then checks whether the entered value is either positive or negative or zero using Else If Statement in C.

#include <stdio.h>

int main()
{
 int number;

 printf(" Enter any number:\n ");
 scanf("%d",&number);

 if (number > 0)
   printf("%d is positive number", number);

 else if (number < 0)
   printf("%d is a negative number.", number);

 else
   printf(" You entered value zero.");

 return 0;
}
C program to find Positive or Negative Number 2a

Check for Negative Value

 Enter any number:
 -11
-11 is a negative number.

Check for Zero

 Enter any number:
 0
 You entered value zero.

Above C program to find Positive or Negative Number is structured as if the first condition fails, it will traverse to the second condition. If both the conditions fail, the else statement will execute. Let’s see the working principle of both the conditions below

  • First Condition(if (number > 0)): Checks whether the given number is greater than 0. If this condition is true, the given value is a positive number.
  • Second condition(else if (number < 0)): Checks whether the given number is less than 0. If this condition is true, it is a negative number
  • If both the above conditions fail, the number is equal to 0.

C program to find Positive or Negative Number using Conditional Operator

This program lets the user enter any number. Next, this c program checks whether the given value is positive or negative or zero using Conditional Operator or Ternary operator

#include <stdio.h>

int main()
{
  int number;

  printf(" Enter any number \n");
  scanf("%d",&number);
 
 (number > 0)? printf(" Number is Positive \n"):
    (number < 0)? printf(" Number is Negative \n"):printf(" Number is zero \n");

  return 0;
}
 Enter any number 
12
 Number is Positive 

Check for Negative Value

 Enter any number 
-19
 Number is Negative 

Check for Zero

 Enter any number 
0
 Number is zero 

Within this C program to find Positive or Negative Number, Let us look closer into the conditional operator in the above code. Here we used a nested conditional operator rather than the normal one.

 (number > 0)? printf("Number is Positive\n"):
    (number < 0)?printf("Number is Negative\n"):printf("Number is zero\n");

If the condition (number > 0) is true, the first statement will execute. And, if it fails, it will check the second condition, which is after the : symbol (number < 0).

  • If this condition is true, it will print the first condition printf(“\n number is Negative Number”)
  • and, If this condition is false, printf(“\n number is zero”); will print.