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:
In this C program, we have to check whether the given number is Positive or Negative.
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.
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.
#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);
}
Enter a number -5 -5, is a negative number RUN2 Enter a number 89 89, is a positive number