![]() |
VOOZH | about |
Write a C program to check whether a given number is positive, negative, or zero.
Examples
Input: 10
Output: Positive
Explanation: Since 10 is greater than 0, it is positive.Input: -5
Output: Negative
Explanation: Since -5 is less than 0, it is negative.
There are two ways to check whether a given number is positive, negative, or zero:
Table of Content
The idea is to use simple if-else-if ladder to check for the equality manually. First use if statement for zero, then else if statement of less than zero numbers (negative) and finally if nothing matches, then it is positive numbers.
Positive
Time Complexity: O(1)
Auxiliary Space: O(1)
The most significant bit (MSB) in the binary representation of an integer is the sign bit. For positive numbers, the sign bit is 0, and for negative numbers, the sign bit is 1. By extracting the MSB from the number, we can determine the sign of the given number. For zero, we have to check it in the same way as previous method.
Positive
Time Complexity: O(1)
Auxiliary Space: O(1)