Java Program to Check if Number is Positive or Negative

In this post, we will write three java programs, first java program check whether the specified number is positive or negative. The second program will take user input using Scanner and check whether it is positive or negative and display the result. And the third program will check the number using nested if-else.

How the program will work?

  • if number is greater than zero “0” then it positive number.
  • if number is smaller than zero “0” then it negative number.
  • if number is equal to zero “0” then it not positive nor negative.

Lets write this logic in java Program.

Example:

num = -23
if num>0
positive number
else if num<0
negative number
else
not positive nor negative

Example 1: Program to check if the number is Positive or Negative

In this program, we have a predefined value of number check whether the specified number is a positive number or negative number. To understand this program you should have the basic knowledge of if-else in Java programming.

public class num_positive_or_negative {
    public static void main(String[] args) {
        int num = -29

        if(num>0){
            System.out.println("Positive number");
        }
        else {
            if(num<0){
                System.out.println("Negative number");
            }
            else {
                System.out.println("Not positive nor negative");
            }
        }
    }
}

Output:

Negative number

Example 2: Program to check if the number is Positive or Negative using scanner

In this program, we will used Scanner to read the number entered by user and then the program checks and displays the result.

import java.util.Scanner;

public class num_positive_or_negative {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        if(num>0){
            System.out.println("Positive number");
        }
        else {
            if(num<0){
                System.out.println("Negative number");
            }
            else {
                System.out.println("Not positive nor negative");
            }
        }
    }
}

Output:

15
Positive number

Example 3: Program to check if the number is Positive or Negative using Nested if-else

Here we are using nested if-else to compare user input number and check if the number is positive or negative. To understand this program you should have the basic knowledge of nest if-else in Java programming.

import java.util.Scanner;

public class num_positive_or_negative {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        if(num>0){
            System.out.println("Positive number");
        }
        else if(num<0){
            System.out.println("Negative number");
        }
        else {
            System.out.println("Not postive nor negative");
        }
    }
}

Recommended:

  1. Java program to check if the number is greater than 100 or not
  2. Java program to get birth dates and find who is older
  3. Check if the given number is prime or not in Java
  4. Java Program Count invalid Mobile numbers form list
  5. Java program Fill a bucket with water using a mug
  6.  

Leave a Comment

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

x