Java How To Find Positive or Negative Numbers
Find Out if a Number is Positive or Negative
Find out if a number is positive or negative:
Example
int myNum = 10; // Is this a positive or negative number?
if (myNum > 0) {
System.out.println("The value is a positive number.");
} else if (myNum < 0) {
System.out.println("The value is a negative number.");
} else {
System.out.println("The value is 0.");
}
Try it Yourself »
Explanation: We use simple comparisons with >
and <
.
- If the number is greater than 0, it is positive.
- If the number is less than 0, it is negative.
- If it is neither, it must be exactly 0.