Java – Check whether number is Positive or Negative or Zero ?

In this article, we will write a simple Java program to check whether passed/input number is Positive or Negative or Zero

1. Check Number is Positive or Negative or Zero

  • We will evaluate the given number using if-else statements,
    1. If number is greater-than Zero then number is Positive
    2. Else if number is less-than Zero then number is Negative
    3. Else number is Zero

CheckNumberPositiveOrNegativeOrZero.java

package in.bench.resources.numbers;

public class CheckNumberPositiveOrNegativeOrZero {

	// main method
	public static void main(String[] args) {

		checkNumberPositiveOrNegative(19); // positive
		checkNumberPositiveOrNegative(0); // negative
		checkNumberPositiveOrNegative(-7); // zero
	}


	/**
	 * This method prints whether number is Positive or Negative or Zero
	 * 
	 * @param number
	 */
	public static void checkNumberPositiveOrNegative(int number) {

		if(0 < number) {

			// number is Positive
			System.out.println(number + " is a Positive number !!");
		}
		else if(0 > number) {

			// number is Negative
			System.out.println(number + " is a Negative number !!");
		}
		else {

			// number is Zero
			System.out.println("Number is Zero !!");
		}
	}
}

Output:

19 is a Positive number !!
Number is Zero !!
-7 is a Negative number !!

Happy Coding !!
Happy Learning !!

Java - Check whether number is Even or Odd ?
Java – Swapping two numbers using temporary variable