Q:
Java Program to Check if a Number is Positive or Negative
belongs to collection: Java Number Programs
Java Number Programs
- How to Reverse a Number in Java
- Java Program Number to Word
- Automorphic Number Program in Java
- Peterson Number in Java
- Sunny Number in Java
- Tech Number in Java
- Fascinating Number in Java
- Keith Number in Java
- Neon Number in Java
- Spy Number in Java
- ATM program Java
- Autobiographical Number in Java
- Emirp Number in Java
- Sphenic Number in Java
- Buzz Number Java
- Duck Number Java
- Evil Number Java
- ISBN Number Java
- Krishnamurthy Number Java
- Bouncy Number in Java
- Mystery Number in Java
- Smith Number in Java
- Strontio Number in Java
- Xylem and Phloem Number in Java
- nth Prime Number Java
- Java Program to Display Alternate Prime Numbers
- Java Program to Find Square Root of a Number Without sqrt Method
- Java Program to Swap Two Numbers Using Bitwise Operator
- Java Program to Find GCD of Two Numbers
- Java Program to Find Largest of Three Numbers
- Java Program to Find Smallest of Three Numbers Using Ternary Operator
- Java Program to Check if a Number is Positive or Negative
- Java Program to Check if a Given Number is Perfect Square
- Java Program to Display Even Numbers From 1 to 100
- Java Program to Display Odd Numbers From 1 to 100
- Java Program to Find Sum of Natural Numbers
CheckPositiveOrNegativeExample1.java
Output:
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:
Output 2:
Output 3:
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:
public static double signum(double d)
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:
public static float signum(float f)
Let's use the signum() method in a Java program.
CheckPositiveOrNegativeExample3.java
Output:
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:
public static int signum(int i)
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:
Output 2:
Output 3:
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:
Output 2:
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:
Let's see another logic to check if the number is positive or negative.
CheckPositiveOrNegativeExample7.java
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer