bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

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.");
}

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.

Previous

Java How To Reverse a Number

Next

Java How To Find the Square Root of a Number