In this tutorial, you will learn how to write a Java program to check whether a given number is positive or negative. First, we will understand how to identify positive and negative numbers using a simple condition.
Next, we will write a basic Java program by assigning a number directly to a variable. Finally, we will create an interactive program using the Scanner class to accept a number from the user and determine whether it is positive, negative, or zero.
Understanding Positive and Negative Numbers
Before writing the Java program, let’s understand what positive and negative numbers are.
A positive number is a number greater than zero. For example:
- 5
- 10
- 25
- 100
A negative number is a number less than zero. For example:
- -5
- -10
- -25
- -100
The number 0 is neutral, meaning it is neither positive nor negative.
In Java, we can evaluate this using an if-else statement with the following logic:
If number > 0:
Print "The number is positive."
Else if number < 0:
Print "The number is negative."
Else:
Print "The number is zero."Java Program to Check Whether a Number is Positive or Negative
Problem Statement: Write a Java program to check whether a given number is positive, negative, or zero.
In this first approach, we will assign a value directly to a variable inside the program. This approach is useful for understanding the basic if-else logic in Java.
Program Code:
// Java program to check whether a given number is positive, negative, or zero
public class PositiveNegative {
public static void main(String[] args) {
// Step 1: Declare and initialize the number we want to check
int number = -25;
// Step 2: Check if the number is greater than zero
if (number > 0) {
System.out.println("The number is positive.");
}
// Step 3: If not greater than zero, check if it is less than zero
else if (number < 0) {
System.out.println("The number is negative.");
}
// Step 4: If it is neither greater nor less than zero, it must be zero
else {
System.out.println("The number is zero.");
}
}
}Expected Output:
The number is negative.
In this example program:
- The number variable stores the value -25.
- The if statement checks whether the number is greater than 0.
- If the condition
number > 0is true, the program displays that the number is positive. - The else if statement checks whether the number is less than 0.
- If the condition
number < 0is true, the program displays that the number is negative. - If neither condition is true, the number must be 0, so the else block displays that the number is zero.
Java Program to Check Positive or Negative Number Using Scanner
In the previous program, we have assigned a predefined value to the variable number. However, we may want to allow the user to enter a number in the practical program. We can use the Scanner class from the java.util package to accept input from the user.
Problem Statement: Write an interactive Java program to accept a number from the user and check whether it is positive, negative, or zero.
Program Code:
import java.util.Scanner;
public class PositiveNegativeUsingScanner {
public static void main(String[] args) {
// Step 1: Create a Scanner object to read user input from the console
Scanner sc = new Scanner(System.in);
// Step 2: Prompt the user to enter a number
System.out.println("Enter a number: ");
// Step 3: Read the integer input provided by the user and store it in 'number'
int number = sc.nextInt();
// Step 4: Check whether the number is positive, negative, or zero
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
// Step 5: Close the Scanner object to free up system resources
sc.close();
}
}Expected Output:
Enter a number: 20 The number is positive.
In this example program:
- The Scanner class is imported from the java.util package to accept input from the user.
- Then, we have created a Scanner object named sc to read input from the user’s keyboard.
- The nextInt() method reads the integer entered by the user and stores it in the number variable of the int data type.
- The if statement checks whether the entered number is greater than zero.
- The else if statement checks whether the number is less than zero.
- If both conditions are false, the entered number is zero.
- Finally, the sc.close() statement closes the Scanner object after the input operation is complete.
How the Program Works
Let’s trace how the program executes for different user inputs:
Case 1: If the user enters -15
- The if condition (number > 0) is false because -15 is not greater than zero.
- The else if condition (number < 0) is true, so the program prints “The number is negative.”.
Case 2: If the user enters 20
- The if condition (number > 0) is true, so the program immediately prints “The number is positive.” and skips the rest of the checks.
Case 3: If the user enters 0
- Both the if and else if conditions evaluate to false.
- The else block executes by default, printing “The number is zero.”.


