C Program to check whether a number is negative or positive

For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:

What are positive and negative numbers?

In this C program, we have to check whether the given number is positive or negative. A positive number is a number that is greater than zero. Positive numbers are written as just numbers, for example, 2,3, 4, etc. A negative number is a number that is smaller than zero. Negative numbers are written with a minus sign before the number. Example : -8, -10 etc.

How to check whether a number is positive or negative?

C programs to find negative or positive numbers have very simple logic. We just take the input number from the user and check whether the number is greater than zero, if so print its a positive number else print a negative number. For that, import some header libraries, and then we use an if-else condition statement to check if the number is greater than or less than zero.

We are using if-else C language syntax to check if the number is greater than or less than zero. The syntax of the if-else statement is given below.

If (test expression) 
{ 
    // codes inside the body of if 
}
else
{ 
   // codes inside the body of else
}


if the test expression is true, code inside the if statement is executed and code inside the else statement is skipped, and vice versa. In this way, the if-else statement works in the C program.

ALGORITHM

STEP 1: Include all the needed header libraries into the C program to use the built-in functions inside the C program.

STEP 2: Start the program using the main function. C program starts execution with main().

STEP 3: Define the variables for numbers using int datatype.

STEP 4: Accept the number from the user using the printf and scanf function and save that number in a variable.

STEP 5: Use an if-else condition to check whether the number is greater than or less than zero.

STEP 6: If the number is greater than zero then print the number as positive, else print the number as negative.

C Source Code

                                          #include <stdio.h>

void main() {
  int number;
  printf("Enter a number\n");
  scanf("%d", & number);
  if (number > 0)
    printf("%d, is a positive number\n", number);
  else
    printf("%d, is a negative number\n", number);
}
                                      

OUTPUT

Enter a number
-5
-5, is a negative number

RUN2

Enter a number
89
89, is a positive number