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.
0 comments