---
title: Check Whether a Number is Positive or Negative
url: https://docs.vultr.com/java/examples/check-whether-a-number-is-positive-or-negative
publish_date: 2024-09-27T17:25:03.986662Z
last_updated: 2024-09-27T17:25:03.986662Z
header_image: https://sjc1.vultrobjects.com/docs-main-doc-assets-1/collection-items/425/93a040be-595b-4cfc-82aa-06084caada01.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Check Whether a Number is Positive or Negative

## Introduction
Determining whether a number is positive or negative is a fundamental operation in many programming scenarios, ranging from financial models to physics simulations. In Java, this can be accomplished with a conditional statement that compares the number against zero.

In this article, you will learn how to check if a number is positive or negative using Java. Explore basic conditional checks through a series of examples that illustrate how to handle different number inputs effectively.

## Basic Positive and Negative Check

### Identifying the Nature of a Number
1. Start by defining a method that takes an integer as a parameter.
1. Use an `if-else` statement to check if the number is greater than, less than, or equal to zero.
1. Print the appropriate status of the number.

    ```java
    public class NumberCheck {
        public static void checkNumber(int number) {
            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.");
            }
        }

        public static void main(String[] args) {
            checkNumber(10);  // Example usage
            checkNumber(-5);  // Example usage
            checkNumber(0);   // Example usage
        }
    }
    ```

    This code defines a method `checkNumber` that prints whether a number is positive, negative, or zero. By passing different values to `checkNumber` in the `main` method, different outcomes are demonstrated.

### Enhancements for Float Values
1. Adapt the same logic to handle float or double values.
1. Note that with floating point numbers, considerations for very small values close to zero might be needed depending on the application.

    ```java
    public class DecimalNumberCheck {
        public static void checkDecimalNumber(double number) {
            if (number > 0.0) {
                System.out.println(number + " is positive.");
            } else if (number < 0.0) {
                System.out.println(number + " is negative.");
            } else {
                System.out.println(number + " is zero.");
            }
        }

        public static void main(String[] args) {
            checkDecimalNumber(3.14);
            checkDecimalNumber(-0.99);
            checkDecimalNumber(0.0);
        }
    }
    ```

    Here, the method `checkDecimalNumber` works similarly to `checkNumber`, but it accepts `double` parameters to accommodate decimal numbers.

## Conclusion
Checking whether a number is positive or negative in Java is straightforward using conditional statements. By setting up simple `if-else` structures, you easily distinguish between positive, negative, and zero values, even with floats or doubles. Implement these techniques in your Java applications to perform essential numeric checks, ensuring robust handling of various numerical inputs.