How to check if a number is positive or negative in java – Java Program to Check If a Number is Positive or Negative

How to check if a number is positive or negative in java: In the previous article, we have discussed Java Program to Check Armstrong Number

In this article we are going to see how we can check whether a number is positive or negative number in Java with examples.

Program to Check If a Number is Positive or Negative

Integers constitute of negative numbers, zero and positive numbers. If we pick a number at random there are 3 possibilities

  1. Number is negative
  2. Number is Zero
  3. Number is positive

So to check if a number is positive or negative we just need to compare the number with zero. If it is greater than 0 then positive or if it is less than zero then it is negative.

 Example :

    5: 5>0 Positive number
-19: -19<0 Negative number
   0: 0 = 0 , It is neither positive, nor negative

Let’s see different ways to check if a number is positive number or negative number.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Approach :

  1. Enter/declare a number and store it. Then the number is compared with zero.
  2. If the number is greater than zero it is positive, if it is smaller than zero it is negative and if it is equal to zero, it is neither positive nor negative.

Method-1:Java Program to Check If a Number is Positive or Negative By Using Static Value

import java.util.Scanner;

public class IntegerNumber
{
    public static void main(String args[])
    {
        //A positive number declared;
        int num1 = 782;

        // Checking whether the number is negative or positive
        if(num1 == 0)
                System.out.println(num1+" is not a positive or negative number.\n It is zero.");       
        else if(num1>0)
                System.out.println(num1+" is a positive number");
        else
                System.out.println(num1+" is a negative number");
                
        //A negative number declared;
        int num2 = -8;

        // Checking whether the number is negative or positive
        if(num2 == 0)
                System.out.println(num2+" is not a positive or negative number.\n It is zero.");       
        else if(num2>0)
                System.out.println(num2+" is a positive number");
        else
                System.out.println(num2+" is a negative number");
    }
}
Output:

782 is a positive number
-8 is a negative number

Method-2:Java Program to Check If a Number is Positive or Negative By User Input Value

import java.util.Scanner;

public class IntegerNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        // Checking whether the number is negative or positive
        if(num == 0)
                System.out.print(num+" is not a positive or negative number.\n It is zero.");       
        else if(num>0)
                System.out.print(num+" is a positive number");
        else
                System.out.print(num+" is a negative number");
    }
}
Output:

Case-1

Enter a number : 879
879 is a positive number

Case-2

Enter a number : -46
-46 is a positive number

Case-3

Enter a number : 0
0 is not a positive or negative number.
It is zero.

Method-3: Java Program to Check If a Number is Positive or Negative By User Defined Method

import java.util.Scanner;

public class IntegerNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        //user defined method numberCheck() called.
        numberChcek(num);
    }
    
    //method tocheck positive or negative number
     public static void numberChcek(int num)
    {
            // Checking whether the number is negative or positive
            if(num == 0)
                    System.out.print(num+" is not a positive or negative number.\n It is zero.");       
            else if(num>0)
                    System.out.print(num+" is a positive number");
            else
                    System.out.print(num+" is a negative number");
    }
}
Output:

Case-1

Enter a number : -46 
-46 is a positive number

Case-2 

Enter a number : 879 
879 is a positive number

Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.

Related Java Programs: