Signup/Sign In
LAST UPDATED: MARCH 5, 2021

Java Program to Check Whether a Number is Positive or Negative

A number is considered to be positive if it is greater than 0 and negative when it is lesser than 0. A point to be noted here is that 0 is neither positive nor negative.

Here, we are given a number and our task is to find out whether the given number is positive or negative.

Input: Enter the number: 56

Output: It is a positive number.

Program 1: To Check Whether a Number is Positive or Negative

In this method, we will use the relational operator to check whether the given number is positive or negative. The main logic behind using the relational operator is that

  • If the number is greater than 0, it is positive.
  • If the number is lesser than 0, it is negative.
  • If a number=0, then it is neither positive nor negative.

Algorithm

  1. Start
  2. Declare a variable.
  3. Initialize the variable.
  4. Use the relational operator to check whether positive or negative.
  5. Display the result.
  6. Stop.

Below is the code for the same.

The below program demonstrates how to use the relational operator to check whether the given number is positive or negative.

//Java Program to check whether the given number is positive or negative
import java.util.Scanner;

public class CheckNumber
{
    // Driver method
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        int num ;     //Number declaration
        System.out.println("Enter the number");
        num=sc.nextInt();    //Number Initialization

        if(num>0)
        System.out.println("It is a positive number");
        else if(num<0)
        System.out.println("It is a negative number");
        else
        System.out.println("Neither positive nor negative");
     } 
}


Enter the number 45
It is a positive number

Program 2: To Check Whether a Number is Positive or Negative

Here, we will use Integer.signum() method to check whether the number is positive or negative. signum() is an inbuilt function that accepts a parameter of integer type. It is used to check whether a number is positive or negative. The logic behind using Integer.signum() method is that,

  • It returns 0 if the argument is 0.
  • It returns 1 if the argument is greater than 0.
  • It returns -1 if the argument is lesser than 0.

Algorithm

  1. Start
  2. Declare a variable.
  3. Initialize the variable.
  4. Call a function to check whether the number is positive or negative.
  5. Use Integer.signum() method to check the result.
  6. Return the result.
  7. Display the result.
  8. Stop.

Below is the code for the same.

The below program demonstrates how to use Integer.signum() method to check whether the given number is positive or negative.

//Java Program to check whether the given number is positive or not
import java.util.Scanner;

public class CheckNumber
{
    //Function Definitin
    static int checkNum(int x)
    { 
        // inbuilt signum function 
        int ans = Integer.signum(x); 
        return ans; 
    } 

    // Driver method
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        int num ;     //Number declaration
        System.out.println("Enter the number");
         num=sc.nextInt();    //Number Initialization
        
        int res=checkNum(num);    //Function Call
        if(res==0)
        System.out.print(num + " is Zero");
        else if (res == 1) 
            System.out.print(num + " is Positive"); 
        else
            System.out.print(num + " is Negative");
    
     }
}


Enter the number -12
-12 is Negative

Program 3: To Check Whether a Number is Positive or Neative

Here, we will use the Bit Shift Operator to check whether a number is positive or negative. The Bit shift operator (Val>>31) copies the highest bit to every other bit. And we know that the highest bit of any negative number is 1, and the highest bit of any other number is 0. Therefore, after (Val>>31) 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.

Algorithm

  1. Start
  2. Declare a variable.
  3. Initialize the variable.
  4. Call a function to check whether the number is positive or negative.
  5. Use the Bit Shift Operator to check the result.
  6. Return the result.
  7. Display the result.
  8. Stop.

Below is the code for the same.

The below program demonstrates how to use the bit shift operator to check whether the given number is positive or negative.

//Java Program to check whether the given number is positive or not
import java.util.Scanner;

public class CheckNumber
{
    //Function Definition
    static String checkPosNeg(int val) 
    { 
  
        String[] result = { "Positive", "Negative" }; 
  
        // checks if the number is positive or negative 
        return result[(val >> 31) & 1]; 
    } 
     
    // Driver method
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        int num ;     //Number declaration
        System.out.println("Enter the number");
        num=sc.nextInt();    //Number Initialization
        
        System.out.println(num + " is " + checkPosNeg(num));   //Function Call
     }     
}


Enter the number 867
867 is Positive



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.