Check whether a number is positive or negative

In this article, we will check whether a number is positive or negative or 0 using C program.

The number to be checked will be taken as an input and compared with 0.
A positive number will be greater than 0 while a negative number will be lesser than 0.

C Program
Below is the C program to check whether a number is positive or negative.

#include<stdio.h>

void main() {
  int num;
  printf("Enter the number to check:\n");
  // read number
  scanf("%d", &num);
  if(num == 0) {
    printf("Number is zero");
  } else if(num > 0) {
    printf("Number is positive");
  } else {
    printf("Number is negative");
  }
}

Output is

Enter the number to check:
23
Number is positive

Enter the number to check:
-22
Number is negative

Enter the number to check:
0
Number is 0

Above program can also be written using nested if statement as shown below

#include<stdio.h> 

void main() { 
  int num; 
  printf("Enter the number to check:\n");
  // read number scanf("%d", &num);
  if(num >= 0) {
    if(num == 0) {
      printf("Number is zero"); 
    } else { 
      printf("Number is positive"); 
    } 
  } else { 
    printf("Number is negative"); 
  } 
}

The first if statement checks whether the number is greater than or equal to zero.
Nested if tests for the number to be 0 or greater than 0 and prints message accordingly.

Hope the article was useful.