Java Program to Check if a Number is Positive or Negative

In this program, we will learn to code the Java Program to Check if a Number is Positive or Negative. Let’s understand Positive and Negative numbers and How to check if a number is Positive or Negative in Java Programming Language?

In this program, we will take the number as input from the user and we will use the if-else conditional statements and check whether the given number is positive or negative.

Now, let’s see the Java Program to Check if a Number is Positive or Negative.

Java Program to Check if a Number is Positive or Negative

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        
        // Checking Number is Positive or Negative
        if(num == 0){
            System.out.println("The given number is 0, which neither a Positive number nor a Negative Number.");
        } else if(num < 0){
            System.out.println("The given number "+num+" is a Negative Number");
        } else{
            System.out.println("The given number "+num+" is a Positive Number");
        }
        
    }
    
}

Output 1

Enter an Integer : 123

The given number 123 is a Positive Number

Output 2

Enter an Integer : -11

The given number -11 is a Negative Number

Output 3

Enter an Integer : 0

The given number is 0, which neither a Positive number nor a Negative Number.

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter an Integer : ");
        int num = sc.nextInt();

In this program, we take the input of the number from the user using the Scanner Class in Java.

        // Checking Number is Positive or Negative
        if(num == 0){
            System.out.println("The given number is 0, which neither a Positive number nor a Negative Number.");
        } else if(num < 0){
            System.out.println("The given number "+num+" is a Negative Number");
        } else{
            System.out.println("The given number "+num+" is a Positive Number");
        }

Then, using the if-else-if ladder we check whether the given number is 0, less than 0, or not. If the given number is 0, then we print that the number is neither a positive nor a negative number. If the number is less than 0 then we print the given number as a Negative number otherwise it is a Positive number.

This is the Java Program to Check if a Number is Positive or Negative.

Conclusion

I hope after going through this post, you understand the Java Program to Check if a Number is Positive or Negative.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

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