Java Program to Check Whether a Number is Positive or Negative

  • Write a Java program to check whether an integer is positive or negative using if else statement.

Given a number N, we have to write a java program to check whether N is positive or negative numbers. If a number is greater than or equal to 0 then it is a positive number otherwise a negative number. There are multiple approaches of testing positive and negative numbers but here we will use if-else statement for decision making.

For Example :
4 >= 0, => 4 is a positive number.
-2 < 0, => -2 is a negative number.

Java program to check for positive and negative number

In this program, we first take an integer as input from user and store it in a variable "num". Then using a if-else statement, we check whether "num" id >= 0 or not. If yes then print appropriate message on screen.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to check whether a number is positive or negative
 */
public class PositiveNegativeNumber {

    public static void main(String[] args) {
        int num;
        Scanner scanner;
        // Take an integer from user
        scanner = new Scanner(System.in);
        System.out.println("Enter an Integer");
        num = scanner.nextInt();

        /*
         * Using if-else statement check whether num is >= 0 or not.
         */
        if (num >= 0) {
            // num is positive
            System.out.println(num + " is Positive Number");
        } else {
            // num is negative
            System.out.println(num + " is Negative Number");
        }
    }
}
Output
Enter an Integer
-4
-4 is Negative Number
Enter an Integer
8
8 is Positive Number

Parameterized Constructor in C++

This java program is similar to above program except here we wrote a function "isPositive" which takes an integer as input and return true or false based of given integer is positive or negative respectively.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to check whether a number is positive or negative using a
 * function
 */

public class PositiveNegativeNumberFunction {

    public static void main(String[] args) {
        int num;
        Scanner scanner;
        // Take an integer from user
        scanner = new Scanner(System.in);
        System.out.println("Enter an Integer");
        num = scanner.nextInt();

        /*
         * Calling isPositive method to check whether num is positive or
         * negative.
         */
        if (isPositive(num)) {
            // num is positive
            System.out.println(num + " is Positive Number");
        } else {
            // num is negative
            System.out.println(num + " is Negative Number");
        }
    }

    /**
     * Returns 1 if num is positive else return 0.
     */
    public static boolean isPositive(int num) {
        if (num >= 0)
            return true;
        else
            return false;
    }
}
Output
 Enter an Integer
-1
-1 is Negative Number
Enter an Integer
3
3 is Positive Number