C Program to check whether a number is negative or positive


April 5, 2022, Learn eTutorial
1388

A number that is greater than Zero is called a Positive number and a number that is less than Zero is called a Negative number. For a better understanding of this Positive or Negative number check 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.

  • Positive number: A positive number can be defined as a number that is greater than zero which is denoted by a '+' sign and normally it is written as numbers. for example 1,2,3,4,...
  • Negative number:  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 using the C program?

C program to find Negative or Positive numbers have very simple logic. We take the input number from the user and check whether the number is greater than zero, if so print it as a Positive number else print a Negative number.

For that, import some header libraries, and then we use a if-else condition statement 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
}

Inside the if-else statement, if the test expression is true, code inside the if statement is executed and code inside the else statement is skipped, and vice versa.

check whether a number is negative or positive - Flowchart

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