In this article, we are going to discuss how can we find out 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 in between any of them then it will be 0.
Example:
Input: 10
Output: Positive number
Input: -12
Output: Negative number
Input: 0
Output: Zero
These are the following ways by which we can find out whether the number is Positive, Negative, or Zero:
- Using if-else statement
- Using ternary operator
- Using switch case
Method 1: Using if-else statement
In 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
Javascript
// Function for number checking function NumberSignChecker(n) { if (n > 0) { console.log( "Positive number" ); } else if (n < 0) { console.log( "Negative number" ); } else { console.log( "Zero" ); } } // Variables holding values let number1 = -5; let number2 = 5; let number3 = 0; NumberSignChecker(number1); // Negative number NumberSignChecker(number2); // Positive number NumberSignChecker(number3); // Zero |
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 operator
In 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 acordingly.
Example: It describes how we can find the given number is Positive, Negative, or Zero using ternary operator
Javascript
let n = 0; // Using ternary operator // for solving the // problem let ans = n < 0 ? "Negative" : n > 0 ? "Positive" : "Zero" ; // Printing result in // console console.log(ans); |
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 case
In 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
Javascript
// JavaScript program for checking if number // is positive, negative or zero let num = 10; // Switch-case statement switch (num / num) { case 1: console.log( "Positive nunber" ); break ; case -1: console.log( "Negative number" ); break ; case 0: console.log( "Zero" ); break ; } |
Positive nunber
Time complexity: O(1), as it is doing constant operations
Space complexity: O(1), as it is using constant space for variables