C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Check if a Number is Positive, Negative, or Zero - Java Program

import java.util.Scanner;

 

public class CheckNumberStatus {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        // Taking user input

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

 

        // Checking conditions

        if (num > 0) {

            System.out.println(num + " is a Positive number.");

        } else if (num < 0) {

            System.out.println(num + " is a Negative number.");

        } else {

            System.out.println("The number is Zero.");

        }

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
Enter a number: 25
25 is a Positive number. 

OUTPUT 2:
Enter a number: -10
-10 is a Negative number.

OUTPUT 3: 
Enter a number: 0
The number is Zero.

Explanation

Step 1: User Input

The user enters any integer value:

int num = sc.nextInt();

Step 2: Condition Check

  1. Positive number
    If the number is greater than 0:

num > 0

  1. Negative number
    If the number is less than 0:

num < 0

  1. Zero
    If both conditions fail, the number must be 0.

Step 3: Output

The program prints whether the number is:
✔ Positive
✔ Negative
✔ Zero

Using if–else if–else ensures only one condition is true and executed.