Java Program to Check if a Given Integer is Positive or Negative
Last Updated :
15 Feb, 2023
Here, the task is to check whether the given integer number is positive or negative. Below are some basic properties of a number.
- If the Integer is greater than zero then it is a positive integer.
- If the number is less than zero then it is a negative integer.
- If the number is equal to zero then it is neither negative nor positive.
Input: X = 12
Output: Positive
Explanation: Value of X is greater than 0 so it is Positive.
Input: x = -5
Output: Negative
Explanation: Value of X less than 0 so it is negative.
Approach 1: Using if statement we can check whether a number is positive or negative or zero.
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
int number =45;
if (number > 0) {
System.out.println(number + " is positive.");
} else if (number < 0) {
System.out.println(number + " is negative.");
} else {
System.out.println(number + " is zero.");
}
}
}
Approach 2: Using Relational operator we can check whether an integer is positive or negative.
- If number>0 then the number is positive.
- If number<0 then the number is negative.
- If a number is neither positive nor negative, the number is equal to 0.
Below is the java implementation of the above approach:
Java
// Java Program to Check if a Given Integer
// is Positive or Negative
import java.io.*;
class GFG {
// Function to check positive and negative
static String checkPosNeg(int x)
{
// checks the number is greater than 0 or not
if (x > 0)
return "Positive";
else if (x < 0)
return "Negative";
else
return "zero";
}
// Driver Function
public static void main(String[] args)
{
// number to be check
int firstNumber = 912;
System.out.println(firstNumber + " is "
+ checkPosNeg(firstNumber));
}
}
Approach 3: Using Integer.signum() Method
Java Integer class provides an inbuilt function signum() to check if a number is positive or negative. It is a static method that accepts a parameter of integer type.
- It returns 0, if the argument is 0.
- It returns 1, if the argument>0.
- It returns -1, if the argument<0.
Syntax:
public static int signum(int i)
Below is the java implementation of the above method.
Java
// Java Program to Check if a Given
// Integer is Positive or Negative
import java.io.*;
class GFG {
// Function to check number is positive or negative
static int checkPosNeg(int x)
{
// inbuilt signum function
int ans = Integer.signum(x);
return ans;
}
// Driver function
public static void main(String[] args)
{
int secondNumber = -125;
int result = checkPosNeg(secondNumber);
if (result == 0)
System.out.print(secondNumber + " is Zero");
else if (result == 1)
System.out.print(secondNumber + " is Positive");
else
System.out.print(secondNumber + " is Negative");
}
}
Approach 4 : 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.
Bit shift operator (Val>>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.
After this 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.
Note: It considers 0 as a positive number.
Below is the Java implementation of the above approach:
Java
// Java Program to Check if a Given
// Integer is Positive or Negative
import java.io.*;
class GFG {
// function to check positive and negative integer
static String checkPosNeg(int val)
{
String[] result = { "Positive", "Negative" };
// checks if the number is positive or negative
return result[(val >> 31) & 1];
}
public static void main(String[] args)
{
int num;
num = -15;
System.out.println(num + " is " + checkPosNeg(num));
}
}
Similar Reads
Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7,
7 min read
Check if Two Integers are Equal or Not in Java Checking two integers equal or not in Java is done by various approaches. Arithmetic operatorComparison OperatorsString functionsXOR operatorComplement (~) and bit-wise (&) operator Example Input: FirstNumber = 15 SecondNumber= 15 Output: Numbers are same Input: FirstNumber = 15 SecondNumber= 25
2 min read
Java Program to convert integer to boolean Given a integer value, the task is to convert this integer value into a boolean value in Java. Examples: Input: int = 1 Output: true Input: int = 0 Output: false Approach: Get the boolean value to be converted. Check if boolean value is true or false If the integer value is greater than equal to 1,
2 min read
How to Handle a java.lang.NegativeArraySizeException in Java? In Java, NegativeArraySizeException is the pre-defined class that can be used to represent the unchecked exception. It is a subclass of the java.lang package, and it can be used to identify that an array was attempted to be created with a negative size, which is not allowed in Java. In this article,
2 min read
Different Methods to Check whether is the number is Square root or not in Java In this article, we will learn different methods to check whether the number is a square root or not in Java. Examples: Input: x = 4Output: 4 is a perfect square: True Input: x = 11Output: 3Explanation: 11 is a perfect square: False Methods to Check Square Root in JavaBelow are the methods by which
5 min read