Here, the task is to check whether the given integer number is positive or negative. Below are some basic properties of a number.
- If the Integer is greater than zero then it is a positive integer.
- If the number is less than zero then it is a negative integer.
- If the number is equal to zero then it is neither negative nor positive.
Input: X = 12 Output: Positive Explanation: Value of X is greater than 0 so it is Positive. Input: x = -5 Output: Negative Explanation: Value of X less than 0 so it is negative.
Approach 1: Using if statement we can check whether a number is positive or negative or zero.
Java
/*package whatever //do not write package name here */ import java.io.*; import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { int number = 45 ; if (number > 0 ) { System.out.println(number + " is positive." ); } else if (number < 0 ) { System.out.println(number + " is negative." ); } else { System.out.println(number + " is zero." ); } } } |
45 is positive.
Approach 2: Using Relational operator we can check whether an integer is positive or negative.
- If number>0 then the number is positive.
- If number<0 then the number is negative.
- If a number is neither positive nor negative, the number is equal to 0.
Below is the java implementation of the above approach:
Java
// Java Program to Check if a Given Integer // is Positive or Negative import java.io.*; class GFG { // Function to check positive and negative static String checkPosNeg( int x) { // checks the number is greater than 0 or not if (x > 0 ) return "Positive" ; else if (x < 0 ) return "Negative" ; else return "zero" ; } // Driver Function public static void main(String[] args) { // number to be check int firstNumber = 912 ; System.out.println(firstNumber + " is " + checkPosNeg(firstNumber)); } } |
912 is Positive
Approach 3: Using Integer.signum() Method
Java Integer class provides an inbuilt function signum() to check if a number is positive or negative. It is a static method that accepts a parameter of integer type.
- It returns 0, if the argument is 0.
- It returns 1, if the argument>0.
- It returns -1, if the argument<0.
Syntax:
public static int signum(int i)
Below is the java implementation of the above method.
Java
// Java Program to Check if a Given // Integer is Positive or Negative import java.io.*; class GFG { // Function to check number is positive or negative static int checkPosNeg( int x) { // inbuilt signum function int ans = Integer.signum(x); return ans; } // Driver function public static void main(String[] args) { int secondNumber = - 125 ; int result = checkPosNeg(secondNumber); if (result == 0 ) System.out.print(secondNumber + " is Zero" ); else if (result == 1 ) System.out.print(secondNumber + " is Positive" ); else System.out.print(secondNumber + " is Negative" ); } } |
-125 is Negative
Approach 4 : Using Bit Shift operator
In Java, the integers are stored in the 2’s complement. We know that the highest bit of any negative number is 1, and the highest bit of any other number is 0.
Bit shift operator (Val>>31) copies the highest bit to every other bit. Therefore, the negative number becomes 11111111 11111111 11111111 11111111, and the positive or zero numbers become 00000000 00000000 00000000 00000000.
After this we can use & operator to check whether a number is positive or negative.
- If ((Val>>31) & 1) is 1 then the number will be negative.
- If ((Val>>31) & 1) is 0 then the number will be positive.
Note: It considers 0 as a positive number.
Below is the Java implementation of the above approach:
Java
// Java Program to Check if a Given // Integer is Positive or Negative import java.io.*; class GFG { // function to check positive and negative integer static String checkPosNeg( int val) { String[] result = { "Positive" , "Negative" }; // checks if the number is positive or negative return result[(val >> 31 ) & 1 ]; } public static void main(String[] args) { int num; num = - 15 ; System.out.println(num + " is " + checkPosNeg(num)); } } |
-15 is Negative