Java Program to Check Whether a Number is Positive or Negative

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Check Whether a Number is Positive or Negative

In this article, we will discuss about how to check whether a number is positive or negative. Before going into the actual topic let’s first know what is this negative and positive number then we will move to next part.

Positive Number : Any number which is greater than 0 is called as positive number.

For example: 12, 566, 980, 34, 7 etc.

Negative Number : Any number which is greater than 0 is called as positive number.

For example: -4, -57, -675, etc.

Note: If a number is  zero. Then it is not positive and not negative.

Now let’s see how to check which is positive and which is negative number.

Method-1 : Using if-else statement (Static Input)

In this program a number is already present, so no need to take user input.

Let’s see the below program to understand it clearly.

public class NumberCheck
{
    public static void main(String[] args) 
    {
        // a number already taken
        // stored in an int variable 'num'
        int num=111;
        
        // If the number is greater than 0 then it is positive
        if(num > 0)
        {
            System.out.println(num+" is a positive number");
        }
        
        // If the number is less than 0 then it is negative
        else if(num < 0)
        {
            System.out.println(num+" is a negative number");
        }
        
        // otherwise it is not positive or negative
        else
        {
            System.out.println(num+" is neither positive nor negative");
        }
    }
}
Output:

111 is a positive number

Method-2 : Using if-else statement (Dynamic Input)

In this program the user has to take a number input. And same by using if-else statement we will check it positive or negative.

import java .util.*;

public class NumberCheck
{
    public static void main(String[] args) 
    {
       // Scanner class object created to take input
        Scanner sc=new Scanner(System.in);
        
        
        System.out.println("Please enter a number");
        
        // Taking input from user
        int num=sc.nextInt();
        
        // If the number is greater than 0 then it is positive
        if(num > 0)
        {
            System.out.println(num+" is a positive number");
        }
        
        // If the number is less than 0 then it is negative
        else if(num < 0)
        {
            System.out.println(num+" is a negative number");
        }
        
        // otherwise it is not positive or negative
        else
        {
            System.out.println(num+" is neither positive nor negative");
        }
    }
}
Output:

CASE-1
Please enter a number:
88
88 is a positive number.

CASE-2
Please enter a number:
-7
-7 is a negative number.

Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.

Related Java Decision Making and Loop Programs: