Check whether a number is positive or negative

Problem statement: Given a number n check whether it’s positive or negative.

Examples:

Example 1:
Input: n=5
Output: Positive

Example2:
Input: n=-6
Output: Negative

Solution

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Solution1: Using conditions

Intuition: As we know if a number is greater than 0 then it is positive otherwise it is negative.

Hence we can compare our number with 0 to get the answer.

Approach:

  • Compare n with zero.
  • If it is greater than 0, it is positive.
  • If it is lesser than 0, it is negative.

For example: 

n=5, 5>0 Hence: Positive

n=-6, -6<0 Hence: Negative

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;
 void check(int n) {
    if (n > 0) {
      cout<<n<<" is positive."<<endl;
    } else {
      cout<<n<<" is negative."<<endl;
    }
  }
  int main() {

    int n = 5;
    check(n);
    n = -6;
    check(n);
  }

Output:

5 is positive.
-6 is negative.

Time complexity: O(1)

Space Complexity: O(1)

Java Code

import java.util.*;

public class tuf {

  public static void main(String[] args) {

    int n = 5;
    check(n);
    n = -6;
    check(n);
  }
  public static void check(int n) {
    if (n > 0) {
      System.out.println(n + " is positive.");
    } else {
      System.out.println(n + " is negative.");
    }
  }
}

Output:

5 is positive.
-6 is negative.

Time complexity: O(1)

Space Complexity: O(1)

Solution 2: Using Bitwise operators

Intuition: The signed right shift operator ‘>>’ uses the sign bit to fill the trailing positions. For example, if the number is positive then 0 will be used to fill the trailing positions and if the number is negative then 1 will be used to fill the trailing positions.

Hence we can say that if a number is positive and we right shift it by 31, then we will get zero, and if the number is negative then we will get -1.

For eg:  a = 5 ,  b = -6

a = 0000 0000 0000 0000 0000 0000 0000 0101 = 5

b = 1111 1111 1111 1111 1111 1111 1111 1010 = -6

a>>31 

a= 0000 0000 0000 0000 0000 0000 0000 0000 = 0

b>>31

b= 1111 1111 1111 1111 1111 1111 1111 1111 = -1

Approach:

  • Right Shift n by 31.
  • If we get 0 then n is positive.
  • If we get -1 then n is negative.

Code:

C++ Code

#include<bits/stdc++.h>
using namespace std;
 void check(int n) {
   if (n >> 31 == 0) {
      cout<<n<<" is positive."<<endl;
    } else {
      cout<<n<<" is negative."<<endl;
    }
  }
  int main() {

    int n = 5;
    check(n);
    n = -6;
    check(n);
  }

Output:

5 is positive.
-6 is negative.

Time complexity: O(1)

Space Complexity: O(1)

Java Code

import java.util.*;

public class tuf {

  public static void main(String[] args) {

    int n = 5;
    check(n);
    n = -6;
    check(n);
  }
  public static void check(int n) {
   if (n >> 31 == 0) {
      System.out.println(n + " is positive.");
    } else if (n >> 31 == -1) {
      System.out.println(n + " is negative.");
    }

}
}

Output:

5 is positive.
-6 is negative.

Time complexity: O(1)

Space Complexity: O(1)

Special thanks to Prashant Sahu for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article