Java 8 – 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 using Java 8 Stream

Positive or Negative or Zero :

  • Using Java 8 Stream
  • Using traditional ifelse construct

1. Using Java 8 Stream :

  • Get the range of numbers from -10 to 10 (negative ten to positive ten) using IntStream.range() method and then iterate these numbers using Stream.forEach() method
  • Inside Stream.forEach() method, evaluate the iterating numbers 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

UsingJava8CheckNumberIsPositiveOrNegativeOrZero.java

package in.bench.resources.arrays.find.number;

import java.util.stream.IntStream;

public class UsingJava8CheckNumberIsPositiveOrNegativeOrZero {

	public static void main(String[] args) {

		// get range of numbers between 0 to 20
		IntStream.range(-10, 10).forEach(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:

-10 is a Negative number !!
-9 is a Negative number !!
-8 is a Negative number !!
-7 is a Negative number !!
-6 is a Negative number !!
-5 is a Negative number !!
-4 is a Negative number !!
-3 is a Negative number !!
-2 is a Negative number !!
-1 is a Negative number !!
Number is Zero !!
1 is a Positive number !!
2 is a Positive number !!
3 is a Positive number !!
4 is a Positive number !!
5 is a Positive number !!
6 is a Positive number !!
7 is a Positive number !!
8 is a Positive number !!
9 is a Positive number !!

2. Using traditional if-else construct :

  • 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 !!

Related Articles :

References :

Happy Coding !!
Happy Learning !!

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