![]() |
VOOZH | about |
Given a number n, the task is to check whether the given number is positive, negative, odd, even, or zero.
Method 1 :
Examples:
Input : 10
Output : Positive number10 is EvenInput : 0
Output : 0 is Even
Positive number 10 is Even
Time complexity: The time complexity of the above code is O(1), as it takes only one step to complete the operation.
Space complexity: The space complexity of the above code is O(1), as no extra space is required in order to complete the operation.
Method 2:
Positive number 20 is Even
Time Complexity: The time complexity of the above program is O(1), as the maximum number of comparisons occurring in the program is constant.
Space Complexity: The space complexity of the above program is O(1), as no extra space is required for the program to run.
Method 3:
Negative number -10 is Even
Time complexity: O(1) as it is doing constant operations
Auxiliary Space: O(1) as it is using constant space for variables
Method 4:
One approach to check if a number is positive, negative, odd, even, or zero without using if-else statements is to use the built-in functions abs, divmod, and isinstance.
Here is an example of how these functions can be used:
positive, odd, non-zero negative, odd, non-zero positive, even, non-zero
The time complexity for this algorithm is O(1), since it does not rely on the size of the input. It only needs to check the properties of the number once and then return the output accordingly.
The space complexity is also O(1), as the algorithm does not use any additional memory, and only returns a constant output.
the abs function returns the absolute value of a number, so abs(num) would return the positive version of num regardless of whether it is positive or negative. The divmod function returns a tuple containing the quotient and remainder of dividing the first argument by the second argument, so divmod(num, 2)[1] would return the remainder of dividing num by 2. The isinstance function returns True if the first argument is an instance of the type specified by the second argument, and False otherwise. In the example above, isinstance(num, (int, float)) would return True if num is an integer or a float, and False otherwise.
Method 5 :
One approach to check if a number is positive, negative, odd, even, or zero without using if-else statements is to use the built-in functions typeid.
Output :
positive, odd, non-zero
negative, odd, non-zero
positive, even, non-zero
Time Complexity : O (1 )
Space Complexity : O(1)