Google News
logo
Java Program to Check Whether a Number is Positive or Negative
In the following example Java program to check whether a number is positive or negative :
Program :
import java.util.Scanner;

public class PositiveNegativeChecker {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number: ");
        double number = input.nextDouble();

        if (number > 0) {
            System.out.println(number + " is positive.");
        } else if (number < 0) {
            System.out.println(number + " is negative.");
        } else {
            System.out.println("The number is zero.");
        }
    }
}
Output :
Enter a number: 12.9
12.9 is positive.
Enter a number: -14.3
-14.3 is negative.
* In this program, we first ask the user to input a number using the Scanner class. Then, we use an if statement to check whether the number is greater than 0. If it is, we print a message indicating that the number is positive.

* If the number is less than 0, we print a message indicating that the number is negative.

* If the number is equal to 0, we print a message indicating that the number is zero.

* We use System.out.println() to print the appropriate message to the console.