Algorithm
- Start
- Declare a variable to store the number.
- Read the number from the user or any other source.
- Check if the number is greater than 0.
- If true, print "The number is positive."
- If false, go to step 5.
- Check if the number is less than 0.
- If true, print "The number is negative."
- If false, print "The number is zero."
- End
Code Examples
#1 Code Example- Check if a Number is Positive or Negative using if else
Code -
Java Programming
public class PositiveNegative {
public static void main(String[] args) {
double number = 12.3;
// 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.");
}
}
Copy The Code &
Try With Live Editor
Output
12.3 is a positive number.
If you change the value of number to a negative number (say -12.3), the output will be:
-12.3 is a negative number.
If you change the value of number to a negative number (say -12.3), the output will be:
-12.3 is a negative number.
Demonstration
Java Programing Example to Check Whether a Number is Positive or Negative-DevsEnv