Java program to check whether a number is positive or negative

Write a java program to check if number is positive or negative

Here, we will write java program to checks whether the specified number is positive or negative using the if else statements.

  • If a number is less than zero, then it is a negative number
  • If a number is greater than zero, then it is a negative number
  • If a number is equal to zero, then it is neither negative nor positive number

  • Program:

public class PositiveNegative {

    public static void main(String[] args) {

        double number = 11.1;

        // true if number is less than 0
        if (number < 0.0)
            System.out.println(number + " is a negative number.");

        // true if number is greater than 0
        else if ( number > 0.0)
            System.out.println(number + " is a positive number.");

        // if both test expression is evaluated to false
        else
            System.out.println(number + " is 0.");
    }
}

  • Output:


11.1 is a positive number.

----- OR -----

-11.1 is a negative number.

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here

0 comments