In this section, we will explore Java programs to determine whether a number is positive or negative. We will employ the following approaches to check the number’s sign:
- Using Relational Operator
- Using Math.signum() Method
- Using Integer.signum() Method
- Using Bit Shift Operator
- Using ArrayList class
Using Relational Operator
To determine whether a number is positive or negative, we have implemented the following logic in our Java program.
- If the number>0 the number is positive.
- If the number<0 the number is negative.
- If a number is neither positive nor negative, the number is equal to 0.
In the provided program, we have initialized a number and utilized the if-else statement to determine whether the number is positive or negative.
CheckPositiveOrNegativeExample.java
public class CheckPositiveOrNegativeExample { public static void main(String[] args) { //number to be check int num=912; //checks the number is greater than 0 or not if(num>0) { System.out.println("The number is positive."); } //checks the number is less than 0 or not else if(num<0) { System.out.println("The number is negative."); } //executes when the above two conditions return false else { System.out.println("The number is zero."); } } }
Output:
The number is positive.
In the given program, we prompt the user to enter a number. Subsequently, we employ the if-else statement to determine whether the provided number is positive or negative.
CheckPositiveOrNegativeExample1.java
import java.util.Scanner; public class CheckPositiveOrNegativeExample1 { public static void main(String[] args) { int num; //object of the Scanner class Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); //reading a number from the user num = sc.nextInt(); //checks the number is greater than 0 or not if(num>0) { System.out.println("The number is positive."); } //checks the number is less than 0 or not else if(num<0) { System.out.println("The number is negative."); } //executes when the above two conditions return false else { System.out.println("The number is zero."); } } }
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 alternative method to check whether a number is positive or negative. In Java, the Math class offers the signum() method specifically for this purpose. The signum() method is a static method that takes a double-type parameter.
Syntax:
public static double signum(double d)
The signum() method returns the signum function of the argument, as described below:
- 0.0: if the argument is 0.
- 1.0: if the argument is greater than 0.
- -1.0: if the argument is less than 0.
Special Cases:
NaN: if the argument is NaN.
Argument: if the argument is positive or negative zero.
Additionally, the Math class provides an overloaded version of the signum() method that accepts a float value instead of a double.
Syntax:
public static float signum(float f)
CheckPositiveOrNegativeExample2.java
import java.util.Scanner; import java.lang.Math.*; public class CheckPositiveOrNegativeExample2 { public static void main(String[] args) { double num, result; //object of the Scanner class Scanner sc = new Scanner(System.in); System.out.print("Enter a number you want to check: "); //reading an input from the user num = sc.nextDouble(); //invoking signum() method of the Math class result=Math.signum(num); //print the result System.out.print(result); } }
Output:
Enter a number you want to check: -98.6 -1.0
Using Integer.signum() Method
The Java Integer class also includes the signum() method, which serves the purpose of checking whether a number is positive or negative. This static method accepts an integer-type parameter.
Syntax:
public static int signum(int i)
The signum() method of the Integer class returns the signum function of the provided argument ‘i’ according to the following rules:
- 0: if the argument is 0.
- 1: if the argument is greater than 0.
- -1: if the argument is less than 0.
CheckPositiveOrNegativeExample3.java
import java.util.Scanner; import java.lang.Integer.*; public class CheckPositiveOrNegativeExample3 { public static void main(String[] args) { int num, result; //object of the Scanner class Scanner sc = new Scanner(System.in); System.out.print("Enter a number you want to check: "); //taking an integer value from the user num = sc.nextInt(); //invoking signum() method of the Integer class result=Integer.signum(num); //prints the result System.out.print(result); } }
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
To gain additional insights on ‘Java Program to Check if a Number is Positive or Negative’, tutorials.freshersnow.com provides valuable information.