![]() |
In this article, we will discuss how we can determine whether the given number is Positive, Negative, or Zero. As we know if the number is less than 0 then it will be called a “Negative” number if it is greater than 0 then it will be called a “Positive” number. And if it doesn’t lie between them, then it will be 0. Example: Input: 10 These are the following ways by which we can find out whether the number is Positive, Negative, or Zero: Table of Content Method 1: Using if-else statementIn this approach, we are using an if-else statement. in which whatever the condition will get satisfied and return true that code block will get executed and it will print the result in the console. Example: It describes how we can find the given number is Positive, Negative, or Zero using an if-else statement
Output Negative number Positive number Zero 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: Using ternary operatorIn this approach we are using ternary operator. Ternary operator is shorthand of “if-else” statement. If “n<0” is true then it will print “Negative” else it will check for another condition and it will print result accordingly. Example: It describes how we can find the given number is Positive, Negative, or Zero using ternary operator
Output Zero Time complexity: O(1) as it is doing constant operations Space complexity: O(1) as it is using constant space for variables Method 3: Using switch caseIn this method we are using switch case for solving this problem. Switch statement will check the condition and return the result. If any of case is equivalent to that result then that code block will get executed and it will print result in the console else default statement will get executed automatically. Example: It describes how we can find the given number is Positive, Negative, or Zero using switch case
Output Positive nunber Time complexity: O(1), as it is doing constant operations Space complexity: O(1), as it is using constant space for variables Using Math.sign()In this approach we are using the Math.sign() method which returns the sign of a number as -1 for negative numbers, 0 for zero, and 1 for positive numbers. It checks the result of Math.sign(num) to determine the sign of the given number and returns the corresponding string. Example: It describes how we can find the given number is Positive, Negative, or Zero using Math.sign() method.
Output Positive Negative Zero Time complexity: O(1), as it is doing constant operations Space complexity: O(1), as it is using constant space for variables Method 5: Using bitwise operatorsIn this approach, we leverage bitwise operators to check the sign of the integer. Specifically, we use the right shift operator (>>) to extract the sign bit and compare it with 0 to determine whether the number is positive, negative, or zero. Example:
Output Positive number Negative number Zero |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 10 |