Check if a Number is Positive or Negative in C Program
ADVERTISEMENTS
Check if a number is positive or negative in c program.
In this article, you will learn how to check whether the input number is a positive
or negative
number by using the if-else-statement, nested if-else statement, and ternary operator.
Example-1
Enter a Number: 5.8
Output :: POSITIVE NUMBER
Output :: POSITIVE NUMBER
Example-2
Enter a Number: -8
Output :: NEGATIVE NUMBER
Output :: NEGATIVE NUMBER
You should know about the following topics in C programming to understand this program:
- C If-else statement
- C Nested if..if-else statement
- C Ternary operator
- C Bitwise operators
1 - Number is Positive or Negative in C
// Check if a Number is Positive or Negative in C
#include <stdio.h>
// It's the main function of the program
int main()
{
double number;
// Step-1 Take input from user
printf("Enter a Number: ");
scanf("%lf", &number);
printf("Output :: ");
// Step-2 Check if the number is negative
if (number < 0.0)
{
printf("NEGATIVE NUMBER\n");
}
// Step-3 Check if the number is positive
else if (number > 0.0)
{
printf("POSITIVE NUMBER\n");
}
// Step-4 If the previous didn't match then
else
{
printf("You enetered 0\n");
}
return 0;
}
Output
Enter a Number: 5.8
Output :: POSITIVE NUMBER
2 - Number is Positive or Negative in C using Nested if-else
// Check if a Number is Positive or Negative in C Nested if-else
#include <stdio.h>
// It's the main function of the program
int main()
{
double number;
// Step-1 Take input from user
printf("Enter a Number: ");
scanf("%lf", &number);
printf("Output :: ");
// Step-2 Check if the number is negative
if (number < 0.0)
{
printf("NEGATIVE NUMBER\n");
}
else
{
// Step-3 Check if the number is positive
if (number > 0.0)
{
printf("POSITIVE NUMBER\n");
}
// Step-4 If the previous didn't match then
else
{
printf("You enetered 0\n");
}
}
return 0;
}
Output
Enter a Number: 0
Output :: You enetered 0
3 - Number is Positive or Negative in C using Ternary Operator
// Check if a Number is Positive or Negative in C using Ternary Operator
#include <stdio.h>
// It's the main function of the program
int main()
{
double number;
// Step-1 Take input from user
printf("Enter a Number: ");
scanf("%lf", &number);
printf("Output :: ");
// Step-2 Use the ternary operator to match the value
number < 0.0
?
printf("NEGATIVE NUMBER\n")
:
(
number > 0.0
?
printf("POSITIVE NUMBER\n")
:
printf("You enetered 0\n")
);
return 0;
}
Output
Enter a Number: -8
Output :: NEGATIVE NUMBER
4 - Number is Positive or Negative in C using Bitwise Operators
Note- To solve this problem, we'll extract the
MSB
(a most significant bit in the binary representation) of input number and It will return the value of the sign bit. Returned value either 1
or 0
.0 => Positive Number
1 => Negative Number
// Check if a Number is Positive or Negative in C using Bitwise Operators
#include <stdio.h>
// It's the main function of the program
int main()
{
int number;
// Step-1 Take input from user
printf("Enter a Number: ");
scanf("%d", &number);
printf("Output :: ");
// Step-2 Check if the number is equal to Zero
if (number == 0)
{
printf("You enetered 0\n");
}
else
{
// Step-3 Extracting the MSB (most significant bit) of the input number
int msb_num = number & (1 << (sizeof(int) * 8 - 1));
if (msb_num)
printf("NEGATIVE NUMBER\n");
else
printf("POSITIVE NUMBER\n");
}
return 0;
}
Output
Enter a Number: -6
Output :: NEGATIVE NUMBER