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

As a Java programmer, it’s important to know how to check whether a number is positive or negative.

This can be easily done using a simple if-else statement in Java.


Here’s the Java code to check whether a number is positive or negative:

import java.util.Scanner;

public class PositiveNegative {
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter a number: ");
       int num = input.nextInt();
       if(num > 0) {
           System.out.println("The number is positive.");
       } else if(num < 0) {
           System.out.println("The number is negative.");
       } else {
           System.out.println("The number is zero.");
       }
   }
}

In this program, we first import the Scanner class from the java.util package to get user input.

We then prompt the user to enter a number using the System.out.print() method and store the input in the variable num.

We then use an if-else statement to check whether the number is positive, negative, or zero.

If the number is greater than zero, we print the message “The number is positive.”

If the number is less than zero, we print the message “The number is negative.”

If the number is equal to zero, we print the message “The number is zero.”

Once we have written the program, we can compile and run it to test whether it works correctly.

If everything is working properly, we should be able to enter a number and see the appropriate message displayed on the screen.


Overall, checking whether a number is positive or negative is a simple task that can be accomplished using basic if-else statements in Java.

By understanding how to perform this task, we can create more complex programs that make use of similar logic.