We are uploading our contents, please refresh app.
Java Premium

Java Program to Check Whether a Number is Positive or Negative

In this program, you'll learn to check whether a given number is positive or negative. This is done by using a if else statement in Java.
Programming code:
import java.util.*;
public class Positive_Negative
{
public static void main(String[] args)
{
double number;
Scanner sc=new Scanner(System.in);
System.out.println("Input a number check positive or negetive:");
number=sc.nextDouble();
// 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:
When you run the program, the output will be:
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 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.

Online user

Download our android app Download App Submit your query
x

Get Updates On

Java & Premium Programs

Java Solved & Questions

Java Coding easy to use

Proper Coding

Straight Into Your INBOX!

We are going to send you our resources for free. To collect your copy at first, join our mailing list. We are promising not to bother you by sending useless information. So don't miss any updates, stay connected!