Javatpoint Logo
Javatpoint Logo

Java Program to Check if a Number is Positive or Negative

In this section, we will write the Java programs to check if a number is positive or negative. We have used the following ways to check the number is positive, negative, or zero.

  • Using Relational Operator
  • Using Math.signum() Method
  • Using Integer.signum() Method
  • Using Bit Shift Operator
  • Using ArrayList class

Using Relational Operator

To check the positive and negative of a number, we have implemented the following logic in the Java program.

  • If number>0 the number is positive.
  • If number<0 the number is negative.
  • If a number is neither positive nor negative, the number is equal to 0.

Let's implement the above logic in a Java program using the if-else statement.

In the following program, we have initialized a number and used the if-else statement to check if a number is positive or negative.

CheckPositiveOrNegativeExample1.java

Output:

The number is positive.

In the following program, we have taken a number from the user and used the if-else statement to check if a number is positive or negative.

CheckPositiveOrNegativeExample2.java

Output 1:

Enter a number: 23
The number is positive.

Output 2:

Enter a number: -499
The number is negative.

Output 3:

Enter a number: 0
The number is zero.

Using Math.signum() Method

There is an alternate way to check if a number is positive or negative. Java Math class provides the signum() method to check if a number is positive or negative. It is a static method that accepts a parameter of double type.

Syntax:

Where d is a parameter whose signum is to be returned. It returns the signum function of the argument, as follows:

0.0: if the argument is 0.
1.0: if the argument>0.
-1.0: if the argument<0.

Special Cases:

NaN: if the argument is NaN.

Argument: if the argument is positive or negative zero.

It is an overloaded method, so the Math class also provides a signum() method that accepts a float value to check if a number is positive or negative.

Syntax:

Let's use the signum() method in a Java program.

CheckPositiveOrNegativeExample3.java

Output:

Enter a number you want to check: -98.6
-1.0

Using Integer.signum() Method

Java Integer class also provides the signum() method to check if a number is positive or negative. It is a static method that accepts a parameter of integer type.

Syntax:

Where i is a parameter whose signum is to be returned. It returns the signum function of the argument, as follows:

the argument, as follows:
0: if the argument is 0.
1: if the argument>0.
-1: if the argument<0.

Let's use the Integer.signum() method in a Java program.

CheckPositiveOrNegativeExample4.java

Output 1:

Enter a number you want to check: 99
1

Output 2:

Enter a number you want to check: -99
-1

Output 3:

Enter a number you want to check: 0
0

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.

In the following program, the bit shift operator (num>>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. The operator & sets the lowest bit to 0. Hence, the combination of [(num >> 31) & 1] reads only the highest bit of num. Note that it considers 0 as a positive number.

CheckPositiveOrNegativeExample5.java

Output 1:

Enter a number you want to check: -98
Negative

Output 2:

Enter a number you want to check: 0
Positive

Let's see another logic to check if the number is positive or negative.

Using ArrayList Class

In the following example, we have created a static method named positiveOrNegative(). It accepts a parameter n of type int. We have created an object of ArrayList class for storing the result positive, negative, and zero. After that, a for loop is used that populates the ArrayList with elements Positive for n elements. If n is positive n will be the index in the ArrayList and return the element Positive as the result. If n is negative, it will never exist in an index of the ArrayList.

CheckPositiveOrNegativeExample6.java

Output:

Positive
Zero
Negative

Let's see another logic to check if the number is positive or negative.

CheckPositiveOrNegativeExample7.java

Output:

Positive
Negative

In this section, we have discussed a lot of ways to check if a number is positive or negative. But we recommend you to use the relation operator to check the number is positive or negative.


Next TopicJava Programs





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA