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

  1. public class CheckPositiveOrNegativeExample1  
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. //number to be check  
  6. int num=912;  
  7. //checks the number is greater than 0 or not  
  8. if(num>0)  
  9. {  
  10. System.out.println(“The number is positive.”);  
  11. }  
  12. //checks the number is less than 0 or not  
  13. else if(num<0)  
  14. {  
  15. System.out.println(“The number is negative.”);  
  16. }  
  17. //executes when the above two conditions return false  
  18. else  
  19. {  
  20. System.out.println(“The number is zero.”);  
  21. }  
  22. }  
  23. }  

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

  1. import java.util.Scanner;  
  2. public class CheckPositiveOrNegativeExample2  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. int num;  
  7. //object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. System.out.print(“Enter a number: “);  
  10. //reading a number from the user  
  11. num = sc.nextInt();  
  12. //checks the number is greater than 0 or not  
  13. if(num>0)  
  14. {  
  15. System.out.println(“The number is positive.”);  
  16. }  
  17. //checks the number is less than 0 or not  
  18. else if(num<0)  
  19. {  
  20. System.out.println(“The number is negative.”);  
  21. }  
  22. //executes when the above two conditions return false  
  23. else  
  24. {  
  25. System.out.println(“The number is zero.”);  
  26. }  
  27. }  
  28. }  

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:

  1. 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:

  1. public static float signum(float f)  

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

CheckPositiveOrNegativeExample3.java

  1. import java.util.Scanner;  
  2. import java.lang.Math.*;  
  3. public class CheckPositiveOrNegativeExample3  
  4. {  
  5. public static void main(String[] args)   
  6. {  
  7. double num, result;  
  8. //object of the Scanner class   
  9. Scanner sc = new Scanner(System.in);  
  10. System.out.print(“Enter a number you want to check: “);  
  11. //reading an input from the user  
  12. num = sc.nextDouble();  
  13. //invoking signum() method of the Math class  
  14. result=Math.signum(num);  
  15. //print the result  
  16. System.out.print(result);  
  17. }  
  18. }  

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:

  1. 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

  1. import java.util.Scanner;  
  2. import java.lang.Integer.*;  
  3. public class CheckPositiveOrNegativeExample4  
  4. {  
  5. public static void main(String[] args)   
  6. {  
  7. int num, result;  
  8. //object of the Scanner class  
  9. Scanner sc = new Scanner(System.in);  
  10. System.out.print(“Enter a number you want to check: “);  
  11. //taking an integer value from the user  
  12. num = sc.nextInt();  
  13. //invoking signum() method of the Integer class  
  14. result=Integer.signum(num);  
  15. //prints the result  
  16. System.out.print(result);  
  17. }  
  18. }  

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

  1. import java.util.Scanner;  
  2. public class CheckPositiveOrNegativeExample5  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. int num, result;  
  7. //object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. System.out.print(“Enter a number you want to check: “);  
  10. //taking an integer value from the user  
  11. num = sc.nextInt();  
  12. //string array that contains results  
  13. String[] result = {“Positive”, “Negative”};  
  14. //checks if the number is positive or negative  
  15. System.out.println(result [(num >> 31) & 1]);  
  16. }  
  17. }  

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

  1. import java.util.*;  
  2. public class CheckPositiveOrNegativeExample6  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. //invoking method and prints the corresponding result  
  7. //parsing a positive value as a parameter  
  8. System.out.println(positiveOrNegative(42));  
  9. //parsing zero as a parameter  
  10. System.out.println(positiveOrNegative(0));   
  11. //parsing a negative value as a parameter  
  12. System.out.println(positiveOrNegative(-190));   
  13. }  
  14. //method to check if the number is positive or negative  
  15. public static String positiveOrNegative(int n)   
  16. {  
  17. //object of the ArrayList class   
  18. ArrayList<String> result = new ArrayList<String>();  
  19. //if n is 0, the response is Zero  
  20. //adds zero to the list  
  21. result.add(“Zero”);  
  22. //the loop populates the ArrayList with elements “Positive” for n elements  
  23. for(int i=0; i<n; i++)   
  24. {  
  25. //adds result to the list  
  26. result.add(“Positive”);  
  27. }  
  28. String result=””;  
  29. //try-catch block for the get() method because it throws IndexOutOfBoundsException  
  30. try   
  31. {  
  32. //the get() method returns the element at the specified position in this list  
  33. result=result.get(n);  
  34. }   
  35. catch (Exception e)   
  36. {  
  37. //if the index is out of bounds, it must be negative  
  38. result=”Negative”;  
  39. }  
  40. //returns the result  
  41. return result;  
  42. }  
  43. }  

Output:

Positive
Zero
Negative

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

CheckPositiveOrNegativeExample7.java

  1. public class CheckPositiveOrNegativeExample7  
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. System.out.println(positiveOrNegative(111));   
  6. System.out.println(positiveOrNegative(-444));   
  7. }  
  8. //method to check the number positive or negative  
  9. public static String positiveOrNegative(int n)   
  10. {  
  11. //array of result that contains string Negative, Zero, and Positive  
  12. String[] results = {“Negative”, “Zero”, “Positive”};  
  13. //logic to check if the number is positive or negative and return the string as result  
  14. return results[1+(1+((n+1)%n)*((n-1)%n))/n];  
  15. }  
  16. }  

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.

Leave a Comment