Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Java Program To Check Whether A Number Is Positive or Negative

The document describes a Java program that uses if/else statements to check if a number is positive, negative, or zero. The program defines a double variable called number, then uses an if/else chain to output whether the number is positive, negative, or zero by comparing it to 0.0. It demonstrates how changing the number's value results in different outputs identifying it as positive, negative, or zero.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Java Program To Check Whether A Number Is Positive or Negative

The document describes a Java program that uses if/else statements to check if a number is positive, negative, or zero. The program defines a double variable called number, then uses an if/else chain to output whether the number is positive, negative, or zero by comparing it to 0.0. It demonstrates how changing the number's value results in different outputs identifying it as positive, negative, or zero.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Example: Check if a Number is Positive or Negative using if

else

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.");
}
}

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.

In the above program, it is quite clear how the variable number is checked to be positive or
negative, by comparing it to 0.

If you're not sure, here is the breakdown:

If a number is greater than zero, it is a positive number.

If a number is less than zero, it is a negative number.


If a number equals to zero, it is zero.

You might also like