java

Check if a number is positive or negative in Java

In this tutorial, we are going to see how to check if a number is positive or negative in Java.

The following program takes the value entered by the user and checks whether it is positive or negative and displays the result.

  • 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 is zero, it is neither negative nor positive.

 

How to check if a number is positive or negative in Java
public class Main
{
    public static void main(String[] args) 
    {
		   int nbr = 5;
		   if(nbr > 0)
			   System.out.println(nbr+" is a positive number");
		   else if(nbr < 0)
			   System.out.println(nbr+" is a negative number");
		   else   
			   System.out.println(nbr+" is neither positive nor negative");
    }
}

Output:

5 is a positive number
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *